Skip to content

Commit 355a775

Browse files
committed
Update the example in the readme.
Also add a missing Deref so you can use BlobsProtocol as a Store directly.
1 parent c4918c2 commit 355a775

File tree

3 files changed

+30
-17
lines changed

3 files changed

+30
-17
lines changed

README.md

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ This crate is used together with [iroh](https://crates.io/crates/iroh). Connecti
2222

2323
- **Requester:** The side that asks for data. It is initiating requests to one or many providers.
2424

25+
A node can be a provider and a requester at the same time.
2526

2627
## Getting started
2728

@@ -33,31 +34,31 @@ Here is a basic example of how to set up `iroh-blobs` with `iroh`:
3334

3435
```rust
3536
use iroh::{protocol::Router, Endpoint};
36-
use iroh_blobs::{store::Store, net_protocol::Blobs};
37+
use iroh_blobs::{store::mem::MemStore, BlobsProtocol};
3738

3839
#[tokio::main]
3940
async fn main() -> anyhow::Result<()> {
4041
// create an iroh endpoint that includes the standard discovery mechanisms
4142
// we've built at number0
4243
let endpoint = Endpoint::builder().discovery_n0().bind().await?;
4344

44-
// create an in-memory blob store
45-
// use `iroh_blobs::net_protocol::Blobs::persistent` to load or create a
46-
// persistent blob store from a path
47-
let blobs = Blobs::memory().build(&endpoint);
48-
49-
// turn on the "rpc" feature if you need to create blobs and tags clients
50-
let blobs_client = blobs.client();
51-
let tags_client = blobs_client.tags();
45+
// create a protocol handler using an in-memory blob store.
46+
let store = MemStore::new();
47+
let blobs = BlobsProtocol::new(&store, endpoint.clone(), None);
5248

5349
// build the router
5450
let router = Router::builder(endpoint)
5551
.accept(iroh_blobs::ALPN, blobs.clone())
5652
.spawn();
5753

58-
// do fun stuff with the blobs protocol!
54+
let tag = blobs.add_slice(b"Hello world").await?;
55+
println!("We are now serving {}", blobs.ticket(tag).await?);
56+
57+
// wait a second
58+
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
59+
60+
// clean shutdown of router and store
5961
router.shutdown().await?;
60-
drop(tags_client);
6162
Ok(())
6263
}
6364
```
@@ -81,4 +82,4 @@ at your option.
8182

8283
Unless you explicitly state otherwise, any contribution intentionally submitted
8384
for inclusion in this project by you, as defined in the Apache-2.0 license,
84-
shall be dual licensed as above, without any additional terms or conditions.
85+
shall be dual licensed as above, without any additional terms or conditions.

src/lib.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
//! Blobs layer for iroh.
1+
#![doc = include_str!("../README.md")]
2+
//! # Module docs
23
//!
34
//! The crate is designed to be used from the [iroh] crate.
45
//!
@@ -16,7 +17,11 @@
1617
//! requests and store the result in a store, as well as a low level state
1718
//! machine for executing requests.
1819
//!
19-
//! The [api::downloader] module provides a component to download blobs from
20+
//! The client API is available in the [api] module. You can get a client
21+
//! either from one of the [store] implementations, or from the [BlobsProtocol]
22+
//! via a
23+
//!
24+
//! The [downloader](api::downloader) module provides a component to download blobs from
2025
//! multiple sources and store them in a store.
2126
//!
2227
//! [BLAKE3]: https://github.com/BLAKE3-team/BLAKE3-specs/blob/master/blake3.pdf
@@ -35,12 +40,11 @@ pub use net_protocol::BlobsProtocol;
3540
pub mod protocol;
3641
pub mod provider;
3742
pub mod ticket;
38-
pub(crate) mod util;
43+
pub mod util;
3944

4045
#[cfg(test)]
4146
mod tests;
4247

43-
#[doc(hidden)]
4448
pub mod test;
4549

4650
pub use protocol::ALPN;

src/net_protocol.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
//! # }
3737
//! ```
3838
39-
use std::{fmt::Debug, future::Future, sync::Arc};
39+
use std::{fmt::Debug, future::Future, ops::Deref, sync::Arc};
4040

4141
use iroh::{
4242
endpoint::Connection,
@@ -66,6 +66,14 @@ pub struct BlobsProtocol {
6666
pub(crate) inner: Arc<BlobsInner>,
6767
}
6868

69+
impl Deref for BlobsProtocol {
70+
type Target = Store;
71+
72+
fn deref(&self) -> &Self::Target {
73+
&self.inner.store
74+
}
75+
}
76+
6977
impl BlobsProtocol {
7078
pub fn new(store: &Store, endpoint: Endpoint, events: Option<mpsc::Sender<Event>>) -> Self {
7179
Self {

0 commit comments

Comments
 (0)