Skip to content

Commit

Permalink
fix(fs): move FsBlockStore::list outside of async_trait
Browse files Browse the repository at this point in the history
see comments in rs-ipfs#458. it is unknown why this is needed but it does work
away of the compiler error.

it was later found out by bisecting Cargo.lock that this is caused by
async-trait 0.1.42 => 0.1.43 upgrade, but it's very unclear why this
would be caused by the only PR in that release.
  • Loading branch information
koivunej committed Aug 3, 2021
1 parent f403c35 commit 7d6f7a1
Showing 1 changed file with 53 additions and 40 deletions.
93 changes: 53 additions & 40 deletions src/repo/fs/blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,58 @@ impl FsBlockStore {
}
}
}

/// Implementation moved here from inside the `impl BlockStore for FsBlockStore` which is
/// `#[async_trait]`, and somehow the two lifetimes generated by `#[async_trait]` just don't
/// work leading to confusing error when `ipfs` is used as a dependency in an outside of
/// workspace crate.
///
/// See [gist] for the standalone working workaround, error and an expanded version of the
/// original code.
///
/// [gist]: https://gist.github.com/koivunej/d6abccb4133839eeab8b36992f1a95fa
async fn list0(&self) -> Result<Vec<Cid>, Error> {
use futures::future::{ready, Either};
use futures::stream::{empty, TryStreamExt};
use tokio_stream::wrappers::ReadDirStream;

let span = tracing::trace_span!("listing blocks");

async move {
let stream = ReadDirStream::new(fs::read_dir(self.path.clone()).await?);

// FIXME: written as a stream to make the Vec be BoxStream<'static, Cid>
let vec = stream
.and_then(|d| async move {
// map over the shard directories
Ok(if d.file_type().await?.is_dir() {
Either::Left(ReadDirStream::new(fs::read_dir(d.path()).await?))
} else {
Either::Right(empty())
})
})
// flatten each
.try_flatten()
// convert the paths ending in ".data" into cid
.try_filter_map(|d| {
let name = d.file_name();
let path: &std::path::Path = name.as_ref();

ready(if path.extension() != Some("data".as_ref()) {
Ok(None)
} else {
let maybe_cid = filestem_to_block_cid(path.file_stem());
Ok(maybe_cid)
})
})
.try_collect::<Vec<_>>()
.await?;

Ok(vec)
}
.instrument(span)
.await
}
}

#[async_trait]
Expand Down Expand Up @@ -355,46 +407,7 @@ impl BlockStore for FsBlockStore {
}

async fn list(&self) -> Result<Vec<Cid>, Error> {
use futures::future::{ready, Either};
use futures::stream::{empty, TryStreamExt};
use tokio_stream::wrappers::ReadDirStream;

let span = tracing::trace_span!("listing blocks");

async move {
let stream = ReadDirStream::new(fs::read_dir(self.path.clone()).await?);

// FIXME: written as a stream to make the Vec be BoxStream<'static, Cid>
let vec = stream
.and_then(|d| async move {
// map over the shard directories
Ok(if d.file_type().await?.is_dir() {
Either::Left(ReadDirStream::new(fs::read_dir(d.path()).await?))
} else {
Either::Right(empty())
})
})
// flatten each
.try_flatten()
// convert the paths ending in ".data" into cid
.try_filter_map(|d| {
let name = d.file_name();
let path: &std::path::Path = name.as_ref();

ready(if path.extension() != Some("data".as_ref()) {
Ok(None)
} else {
let maybe_cid = filestem_to_block_cid(path.file_stem());
Ok(maybe_cid)
})
})
.try_collect::<Vec<_>>()
.await?;

Ok(vec)
}
.instrument(span)
.await
self.list0().await
}

async fn wipe(&self) {
Expand Down

0 comments on commit 7d6f7a1

Please sign in to comment.