Skip to content

Commit

Permalink
feat: Make futures Send and Sync
Browse files Browse the repository at this point in the history
  • Loading branch information
matheus23 committed Dec 29, 2023
1 parent 2d3517f commit 0b3685a
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 5 deletions.
28 changes: 28 additions & 0 deletions car-mirror/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -450,3 +450,31 @@ impl Default for Config {
}
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::test_utils::assert_send_sync;

#[cfg(not(target_arch = "wasm32"))]
fn send_sync_tests() {
assert_send_sync(|| {
block_send(
unimplemented!(),
unimplemented!(),
unimplemented!(),
unimplemented!(),
unimplemented!(),
)
});
assert_send_sync(|| {
block_receive(
unimplemented!(),
unimplemented!(),
unimplemented!(),
unimplemented!(),
unimplemented!(),
)
})
}
}
2 changes: 2 additions & 0 deletions car-mirror/src/test_utils/local_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,5 @@ pub(crate) async fn total_dag_blocks(root: Cid, store: &impl BlockStore) -> Resu
.await?
.len())
}

pub(crate) fn assert_send_sync<T: Send + Sync>(fut: fn() -> T) {}
13 changes: 8 additions & 5 deletions car-mirror/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::common::references;
use anyhow::Result;
use async_trait::async_trait;
use libipld::{Cid, IpldCodec};
use wnfs_common::BlockStore;
use wnfs_common::{utils::CondSync, BlockStore};

/// This trait abstracts caches used by the car mirror implementation.
/// An efficient cache implementation can significantly reduce the amount
Expand All @@ -13,8 +13,9 @@ use wnfs_common::BlockStore;
///
/// See `InMemoryCache` for a `quick_cache`-based implementation
/// (enable the `quick-cache` feature), and `NoCache` for disabling the cache.
#[async_trait(?Send)]
pub trait Cache {
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
pub trait Cache: CondSync {
/// This returns further references from the block referenced by given CID,
/// if the cache is hit.
/// Returns `None` if it's a cache miss.
Expand Down Expand Up @@ -85,7 +86,8 @@ impl InMemoryCache {
}

#[cfg(feature = "quick_cache")]
#[async_trait(?Send)]
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
impl Cache for InMemoryCache {
async fn get_references_cached(&self, cid: Cid) -> Result<Option<Vec<Cid>>> {
Ok(self.references.get(&cid))
Expand All @@ -101,7 +103,8 @@ impl Cache for InMemoryCache {
#[derive(Debug)]
pub struct NoCache;

#[async_trait(?Send)]
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
impl Cache for NoCache {
async fn get_references_cached(&self, _: Cid) -> Result<Option<Vec<Cid>>> {
Ok(None)
Expand Down

0 comments on commit 0b3685a

Please sign in to comment.