Skip to content

Commit

Permalink
Fix SnapshotBlockStore impl
Browse files Browse the repository at this point in the history
  • Loading branch information
fabricedesre committed Nov 7, 2023
1 parent 0ebcbb5 commit 53dcafa
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
2 changes: 1 addition & 1 deletion wnfs-common/src/blockstore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Bytes>;
async fn put_block(&self, bytes: impl Into<Bytes> + Send, codec: u64) -> Result<Cid>;

Expand Down
2 changes: 1 addition & 1 deletion wnfs-common/src/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ mod tests {

#[async_trait]
impl AsyncSerialize for Example {
async fn async_serialize<S, B>(&self, serializer: S, store: &B) -> Result<S::Ok, S::Error>
async fn async_serialize<S, B>(&self, serializer: S, _store: &B) -> Result<S::Ok, S::Error>
where
S: Serializer + Send,
B: BlockStore + ?Sized + Sync,
Expand Down
12 changes: 9 additions & 3 deletions wnfs-common/src/utils/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use libipld::{
prelude::{Decode, Encode},
Cid, Ipld,
};
use parking_lot::Mutex;
use proptest::{
strategy::{Strategy, ValueTree},
test_runner::TestRunner,
Expand All @@ -17,13 +18,18 @@ use serde_json::Value;
use std::{
collections::{BTreeMap, HashMap},
io::Cursor,
sync::Arc,
};

//--------------------------------------------------------------------------------------------------
// Type Definitions
//--------------------------------------------------------------------------------------------------

type BlockHandler = Box<dyn Fn(&Bytes) -> Result<Ipld>>;
pub trait BytesToIpld: Send {
fn convert(&self, bytes: &Bytes) -> Result<Ipld>;
}

type BlockHandler = Arc<Mutex<dyn BytesToIpld>>;

#[derive(Default)]
pub struct SnapshotBlockStore {
Expand Down Expand Up @@ -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!(),
Expand Down Expand Up @@ -100,7 +106,7 @@ impl BlockStore for SnapshotBlockStore {
}

#[inline]
async fn put_block(&self, bytes: impl Into<Bytes>, codec: u64) -> Result<Cid> {
async fn put_block(&self, bytes: impl Into<Bytes> + Send, codec: u64) -> Result<Cid> {
self.inner.put_block(bytes, codec).await
}
}
Expand Down

0 comments on commit 53dcafa

Please sign in to comment.