diff options
Diffstat (limited to 'cache/src')
-rw-r--r-- | cache/src/lib.rs | 19 |
1 files changed, 7 insertions, 12 deletions
diff --git a/cache/src/lib.rs b/cache/src/lib.rs index 9dc2f09..52245d6 100644 --- a/cache/src/lib.rs +++ b/cache/src/lib.rs @@ -5,7 +5,6 @@ */ use anyhow::{Context, anyhow}; use base64::Engine; -use bincode::{Decode, Encode}; use log::{info, warn}; use rand::random; use serde::{Deserialize, Serialize}; @@ -44,7 +43,7 @@ static CONF: LazyLock<Config> = LazyLock::new(|| { .expect("cache config not preloaded. logic error") }); -#[derive(Debug, Encode, Decode, Serialize, Clone, PartialEq, Eq)] +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)] pub struct CachePath(pub PathBuf); impl CachePath { pub fn abs(&self) -> PathBuf { @@ -182,7 +181,7 @@ pub fn cache_memory<Fun, T>( ) -> Result<Arc<T>, anyhow::Error> where Fun: FnMut() -> Result<T, anyhow::Error>, - T: Encode + Decode<()> + Send + Sync + 'static, + T: Serialize + for<'de> Deserialize<'de> + Send + Sync + 'static, { let (_, location) = cache_location(kind, &key); { @@ -201,14 +200,12 @@ where let location = cache_file(kind, &key, move |file| { let object = generate()?; let mut file = std::io::BufWriter::new(file); - bincode::encode_into_std_write(&object, &mut file, bincode::config::standard()) - .context("encoding cache object")?; + serde_json::to_writer(&mut file, &object).context("encoding cache object")?; file.flush()?; Ok(()) })?; let mut file = std::io::BufReader::new(std::fs::File::open(location.abs())?); - let object = bincode::decode_from_std_read::<T, _, _>(&mut file, bincode::config::standard()) - .context("decoding cache object")?; + let object = serde_json::from_reader::<_, T>(&mut file).context("decoding cache object")?; let object = Arc::new(object); let size = file.stream_position()? as usize; // this is an approximation mainly since varint is used in bincode @@ -238,7 +235,7 @@ pub async fn async_cache_memory<Fun, Fut, T>( where Fun: FnOnce() -> Fut, Fut: Future<Output = Result<T, anyhow::Error>>, - T: Encode + Decode<()> + Send + Sync + 'static, + T: Serialize + for<'de> Deserialize<'de> + Send + Sync + 'static, { let (_, location) = cache_location(kind, &key); { @@ -256,8 +253,7 @@ where let location = async_cache_file(kind, &key, move |mut file| async move { let object = generate().await?; - let data = bincode::encode_to_vec(&object, bincode::config::standard()) - .context("encoding cache object")?; + let data = serde_json::to_vec(&object).context("encoding cache object")?; file.write_all(&data).await?; @@ -269,8 +265,7 @@ where file.read_to_end(&mut data) .await .context("reading cache object")?; - let (object, _) = bincode::decode_from_slice::<T, _>(&data, bincode::config::standard()) - .context("decoding cache object")?; + let object = serde_json::from_slice::<T>(&data).context("decoding cache object")?; let object = Arc::new(object); let size = file.stream_position().await? as usize; // this is an approximation mainly since varint is used in bincode |