Skip to content

Commit b2658b6

Browse files
committed
Add example how to use Blobs::reader
1 parent b2b0216 commit b2658b6

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

src/api.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
//! with a remote store via rpc calls.
55
//!
66
//! The entry point for the api is the [`Store`] struct. There are several ways
7-
//! to obtain a `Store` instance: it is available via [`Deref`](std::ops::Deref)
7+
//! to obtain a `Store` instance: it is available via [`Deref`]
88
//! from the different store implementations
99
//! (e.g. [`MemStore`](crate::store::mem::MemStore)
1010
//! and [`FsStore`](crate::store::fs::FsStore)) as well as on the

src/api/blobs.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,34 @@ impl Blobs {
105105
})
106106
}
107107

108+
/// Create a reader for the given hash. The reader implements [`tokio::io::AsyncRead`] and [`tokio::io::AsyncSeek`]
109+
/// and therefore can be used to read the blob's content.
110+
///
111+
/// Any access to parts of the blob that are not present will result in an error.
112+
///
113+
/// Example:
114+
/// ```rust
115+
/// use iroh_blobs::{store::mem::MemStore, api::blobs::Blobs};
116+
/// use tokio::io::AsyncReadExt;
117+
///
118+
/// # async fn example() -> anyhow::Result<()> {
119+
/// let store = MemStore::new();
120+
/// let tag = store.add_slice(b"Hello, world!").await?;
121+
/// let mut reader = store.reader(tag.hash);
122+
/// let mut buf = String::new();
123+
/// reader.read_to_string(&mut buf).await?;
124+
/// assert_eq!(buf, "Hello, world!");
125+
/// # Ok(())
126+
/// }
127+
/// ```
108128
pub fn reader(&self, hash: impl Into<Hash>) -> BlobReader {
109129
self.reader_with_opts(ReaderOptions { hash: hash.into() })
110130
}
111131

132+
/// Create a reader for the given options. The reader implements [`tokio::io::AsyncRead`] and [`tokio::io::AsyncSeek`]
133+
/// and therefore can be used to read the blob's content.
134+
///
135+
/// Any access to parts of the blob that are not present will result in an error.
112136
pub fn reader_with_opts(&self, options: ReaderOptions) -> BlobReader {
113137
BlobReader::new(self.clone(), options)
114138
}

src/api/blobs/reader.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use crate::api::{
1111
proto::ExportRangesItem,
1212
};
1313

14+
/// A reader for blobs that implements `AsyncRead` and `AsyncSeek`.
1415
#[derive(Debug)]
1516
pub struct BlobReader {
1617
blobs: Blobs,
@@ -298,7 +299,6 @@ mod tests {
298299
async fn reader_partial_fs() -> TestResult<()> {
299300
let testdir = tempfile::tempdir()?;
300301
let store = FsStore::load(testdir.path().to_owned()).await?;
301-
// reader_smoke_raw(store.blobs()).await?;
302302
reader_partial(store.blobs()).await?;
303303
Ok(())
304304
}
@@ -314,7 +314,6 @@ mod tests {
314314
async fn reader_smoke_fs() -> TestResult<()> {
315315
let testdir = tempfile::tempdir()?;
316316
let store = FsStore::load(testdir.path().to_owned()).await?;
317-
// reader_smoke_raw(store.blobs()).await?;
318317
reader_smoke(store.blobs()).await?;
319318
Ok(())
320319
}

0 commit comments

Comments
 (0)