From 53dcafac5122ea6491e71838f0438907c6cc93e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabrice=20Desr=C3=A9?= Date: Tue, 7 Nov 2023 10:26:43 -0800 Subject: [PATCH] Fix SnapshotBlockStore impl --- wnfs-common/src/blockstore.rs | 2 +- wnfs-common/src/link.rs | 2 +- wnfs-common/src/utils/test.rs | 12 +++++++++--- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/wnfs-common/src/blockstore.rs b/wnfs-common/src/blockstore.rs index 8a7bbb45..d3f72e3d 100644 --- a/wnfs-common/src/blockstore.rs +++ b/wnfs-common/src/blockstore.rs @@ -47,7 +47,7 @@ pub const CODEC_RAW: u64 = 0x55; /// For types that implement block store operations like adding, getting content from the store. #[async_trait] -pub trait BlockStore: Sized { +pub trait BlockStore: Sized + Send { async fn get_block(&self, cid: &Cid) -> Result; async fn put_block(&self, bytes: impl Into + Send, codec: u64) -> Result; diff --git a/wnfs-common/src/link.rs b/wnfs-common/src/link.rs index 35d0bd93..92c55052 100644 --- a/wnfs-common/src/link.rs +++ b/wnfs-common/src/link.rs @@ -284,7 +284,7 @@ mod tests { #[async_trait] impl AsyncSerialize for Example { - async fn async_serialize(&self, serializer: S, store: &B) -> Result + async fn async_serialize(&self, serializer: S, _store: &B) -> Result where S: Serializer + Send, B: BlockStore + ?Sized + Sync, diff --git a/wnfs-common/src/utils/test.rs b/wnfs-common/src/utils/test.rs index c6db5086..33bd6d33 100644 --- a/wnfs-common/src/utils/test.rs +++ b/wnfs-common/src/utils/test.rs @@ -8,6 +8,7 @@ use libipld::{ prelude::{Decode, Encode}, Cid, Ipld, }; +use parking_lot::Mutex; use proptest::{ strategy::{Strategy, ValueTree}, test_runner::TestRunner, @@ -17,13 +18,18 @@ use serde_json::Value; use std::{ collections::{BTreeMap, HashMap}, io::Cursor, + sync::Arc, }; //-------------------------------------------------------------------------------------------------- // Type Definitions //-------------------------------------------------------------------------------------------------- -type BlockHandler = Box Result>; +pub trait BytesToIpld: Send { + fn convert(&self, bytes: &Bytes) -> Result; +} + +type BlockHandler = Arc>; #[derive(Default)] pub struct SnapshotBlockStore { @@ -59,7 +65,7 @@ impl SnapshotBlockStore { let ipld = match cid.codec() { CODEC_DAG_CBOR => Ipld::decode(DagCborCodec, &mut Cursor::new(bytes))?, CODEC_RAW => match self.block_handlers.get(cid) { - Some(func) => func(bytes)?, + Some(func) => func.lock().convert(bytes)?, None => Ipld::Bytes(bytes.to_vec()), }, _ => unimplemented!(), @@ -100,7 +106,7 @@ impl BlockStore for SnapshotBlockStore { } #[inline] - async fn put_block(&self, bytes: impl Into, codec: u64) -> Result { + async fn put_block(&self, bytes: impl Into + Send, codec: u64) -> Result { self.inner.put_block(bytes, codec).await } }