Skip to content

Commit 91459aa

Browse files
authored
docs: Add module docs (#114)
## Description Adds module docs and includes the README.md into the docs so the example in README.md is being tested. ## Breaking Changes None, I guess. The additional deref is not breaking. ## Notes & open questions There is an unrelated change, adding back lifetimes, the lack of which that make my local clippy unhappy (I only removed the lifetimes because of clippy in the first place). ## Change checklist - [ ] Self-review. - [ ] Documentation updates following the [style guide](https://rust-lang.github.io/rfcs/1574-more-api-documentation-conventions.html#appendix-a-full-conventions-text), if relevant. - [ ] Tests if relevant. - [ ] All breaking changes documented.
1 parent 5ece40f commit 91459aa

File tree

4 files changed

+65
-27
lines changed

4 files changed

+65
-27
lines changed

README.md

Lines changed: 14 additions & 13 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

@@ -31,33 +32,33 @@ Iroh provides a [`Router`](https://docs.rs/iroh/latest/iroh/protocol/struct.Rout
3132

3233
Here is a basic example of how to set up `iroh-blobs` with `iroh`:
3334

34-
```rust
35+
```rust,no_run
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 for control-c
58+
tokio::signal::ctrl_c().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/api/blobs.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ impl Blobs {
127127
.await
128128
}
129129

130-
pub fn add_slice(&self, data: impl AsRef<[u8]>) -> AddProgress {
130+
pub fn add_slice(&self, data: impl AsRef<[u8]>) -> AddProgress<'_> {
131131
let options = ImportBytesRequest {
132132
data: Bytes::copy_from_slice(data.as_ref()),
133133
format: crate::BlobFormat::Raw,
@@ -136,7 +136,7 @@ impl Blobs {
136136
self.add_bytes_impl(options)
137137
}
138138

139-
pub fn add_bytes(&self, data: impl Into<bytes::Bytes>) -> AddProgress {
139+
pub fn add_bytes(&self, data: impl Into<bytes::Bytes>) -> AddProgress<'_> {
140140
let options = ImportBytesRequest {
141141
data: data.into(),
142142
format: crate::BlobFormat::Raw,
@@ -145,7 +145,7 @@ impl Blobs {
145145
self.add_bytes_impl(options)
146146
}
147147

148-
pub fn add_bytes_with_opts(&self, options: impl Into<AddBytesOptions>) -> AddProgress {
148+
pub fn add_bytes_with_opts(&self, options: impl Into<AddBytesOptions>) -> AddProgress<'_> {
149149
let options = options.into();
150150
let request = ImportBytesRequest {
151151
data: options.data,
@@ -155,7 +155,7 @@ impl Blobs {
155155
self.add_bytes_impl(request)
156156
}
157157

158-
fn add_bytes_impl(&self, options: ImportBytesRequest) -> AddProgress {
158+
fn add_bytes_impl(&self, options: ImportBytesRequest) -> AddProgress<'_> {
159159
trace!("{options:?}");
160160
let this = self.clone();
161161
let stream = Gen::new(|co| async move {
@@ -180,7 +180,7 @@ impl Blobs {
180180
AddProgress::new(self, stream)
181181
}
182182

183-
pub fn add_path_with_opts(&self, options: impl Into<AddPathOptions>) -> AddProgress {
183+
pub fn add_path_with_opts(&self, options: impl Into<AddPathOptions>) -> AddProgress<'_> {
184184
let options = options.into();
185185
self.add_path_with_opts_impl(ImportPathRequest {
186186
path: options.path,
@@ -190,7 +190,7 @@ impl Blobs {
190190
})
191191
}
192192

193-
fn add_path_with_opts_impl(&self, options: ImportPathRequest) -> AddProgress {
193+
fn add_path_with_opts_impl(&self, options: ImportPathRequest) -> AddProgress<'_> {
194194
trace!("{:?}", options);
195195
let client = self.client.clone();
196196
let stream = Gen::new(|co| async move {
@@ -215,7 +215,7 @@ impl Blobs {
215215
AddProgress::new(self, stream)
216216
}
217217

218-
pub fn add_path(&self, path: impl AsRef<Path>) -> AddProgress {
218+
pub fn add_path(&self, path: impl AsRef<Path>) -> AddProgress<'_> {
219219
self.add_path_with_opts(AddPathOptions {
220220
path: path.as_ref().to_owned(),
221221
mode: ImportMode::Copy,
@@ -226,7 +226,7 @@ impl Blobs {
226226
pub async fn add_stream(
227227
&self,
228228
data: impl Stream<Item = io::Result<Bytes>> + Send + Sync + 'static,
229-
) -> AddProgress {
229+
) -> AddProgress<'_> {
230230
let inner = ImportByteStreamRequest {
231231
format: crate::BlobFormat::Raw,
232232
scope: Scope::default(),
@@ -521,7 +521,7 @@ pub struct Batch<'a> {
521521
}
522522

523523
impl<'a> Batch<'a> {
524-
pub fn add_bytes(&self, data: impl Into<Bytes>) -> BatchAddProgress {
524+
pub fn add_bytes(&self, data: impl Into<Bytes>) -> BatchAddProgress<'_> {
525525
let options = ImportBytesRequest {
526526
data: data.into(),
527527
format: crate::BlobFormat::Raw,
@@ -530,7 +530,7 @@ impl<'a> Batch<'a> {
530530
BatchAddProgress(self.blobs.add_bytes_impl(options))
531531
}
532532

533-
pub fn add_bytes_with_opts(&self, options: impl Into<AddBytesOptions>) -> BatchAddProgress {
533+
pub fn add_bytes_with_opts(&self, options: impl Into<AddBytesOptions>) -> BatchAddProgress<'_> {
534534
let options = options.into();
535535
BatchAddProgress(self.blobs.add_bytes_impl(ImportBytesRequest {
536536
data: options.data,
@@ -539,7 +539,7 @@ impl<'a> Batch<'a> {
539539
}))
540540
}
541541

542-
pub fn add_slice(&self, data: impl AsRef<[u8]>) -> BatchAddProgress {
542+
pub fn add_slice(&self, data: impl AsRef<[u8]>) -> BatchAddProgress<'_> {
543543
let options = ImportBytesRequest {
544544
data: Bytes::copy_from_slice(data.as_ref()),
545545
format: crate::BlobFormat::Raw,
@@ -548,7 +548,7 @@ impl<'a> Batch<'a> {
548548
BatchAddProgress(self.blobs.add_bytes_impl(options))
549549
}
550550

551-
pub fn add_path_with_opts(&self, options: impl Into<AddPathOptions>) -> BatchAddProgress {
551+
pub fn add_path_with_opts(&self, options: impl Into<AddPathOptions>) -> BatchAddProgress<'_> {
552552
let options = options.into();
553553
BatchAddProgress(self.blobs.add_path_with_opts_impl(ImportPathRequest {
554554
path: options.path,

src/lib.rs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,31 @@
1+
#![doc = include_str!("../README.md")]
2+
//! # Module docs
3+
//!
4+
//! The crate is designed to be used from the [iroh] crate.
5+
//!
6+
//! It implements a [protocol] for streaming content-addressed data transfer using
7+
//! [BLAKE3] verified streaming.
8+
//!
9+
//! It also provides a [store] module for storage of blobs and outboards,
10+
//! as well as a [persistent](crate::store::fs) and a [memory](crate::store::mem)
11+
//! store implementation.
12+
//!
13+
//! To implement a server, the [provider] module provides helpers for handling
14+
//! connections and individual requests given a store.
15+
//!
16+
//! To perform get requests, the [get] module provides utilities to perform
17+
//! requests and store the result in a store, as well as a low level state
18+
//! machine for executing requests.
19+
//!
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
25+
//! multiple sources and store them in a store.
26+
//!
27+
//! [BLAKE3]: https://github.com/BLAKE3-team/BLAKE3-specs/blob/master/blake3.pdf
28+
//! [iroh]: https://docs.rs/iroh
129
mod hash;
230
pub mod store;
331
pub use hash::{BlobFormat, Hash, HashAndFormat};
@@ -11,11 +39,12 @@ mod net_protocol;
1139
pub use net_protocol::BlobsProtocol;
1240
pub mod protocol;
1341
pub mod provider;
14-
pub mod test;
1542
pub mod ticket;
1643
pub mod util;
1744

1845
#[cfg(test)]
1946
mod tests;
2047

48+
pub mod test;
49+
2150
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)