Skip to content

Commit

Permalink
refactor(iroh-router)!: change accept to take an AsRef<[u8]> (#2963)
Browse files Browse the repository at this point in the history
## Description

Change RouterBuilder::accept to take an AsRef<[u8]> instead of a
Vec<u8>. Also change iroh node accept.

## Breaking Changes

- RouterBuilder::accept takes impl AsRef<[u8]>. Existing code should
still work!
- Node::accept takes impl AsRef<[u8]>. Existing code should still work!

## Notes & open questions

Question: since we are making the API more pleasant at the expense of a
bit of inefficiency, should we also change the second parameter to take
a &Arc instead of an Arc?

So
```rust
fn accept(alpn: impl AsRef<[u8]>, proto: &Arc<dyn ProtocolHandler>)
```

In the various examples I found that you very frequently need the
protocol handler again after accept-ing it. So this saves one .clone()
in many cases, at the expense of an arc clone, which we basically treat
as free anyway.

## 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.
  • Loading branch information
rklaehn authored Nov 26, 2024
1 parent a956319 commit 4e3b431
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 5 deletions.
2 changes: 1 addition & 1 deletion iroh-router/examples/custom-protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ async fn main() -> Result<()> {
let builder = Router::builder(endpoint);

// Add our protocol, identified by our ALPN, to the node, and spawn the node.
let router = builder.accept(ALPN.to_vec(), proto.clone()).spawn().await?;
let router = builder.accept(ALPN, proto.clone()).spawn().await?;

match args.command {
Command::Listen { text } => {
Expand Down
4 changes: 2 additions & 2 deletions iroh-router/src/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ impl RouterBuilder {
}
}

pub fn accept(mut self, alpn: Vec<u8>, handler: Arc<dyn ProtocolHandler>) -> Self {
self.protocols.insert(alpn, handler);
pub fn accept(mut self, alpn: impl AsRef<[u8]>, handler: Arc<dyn ProtocolHandler>) -> Self {
self.protocols.insert(alpn.as_ref().to_vec(), handler);
self
}

Expand Down
4 changes: 2 additions & 2 deletions iroh/src/node/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -579,14 +579,14 @@ impl ProtocolBuilder {
/// let handler = MyProtocol { client };
///
/// let node = unspawned_node
/// .accept(MY_ALPN.to_vec(), Arc::new(handler))
/// .accept(MY_ALPN, Arc::new(handler))
/// .spawn()
/// .await?;
/// # node.shutdown().await?;
/// # Ok(())
/// # }
/// ```
pub fn accept(mut self, alpn: Vec<u8>, handler: Arc<dyn ProtocolHandler>) -> Self {
pub fn accept(mut self, alpn: impl AsRef<[u8]>, handler: Arc<dyn ProtocolHandler>) -> Self {
self.router = self.router.accept(alpn, handler);
self
}
Expand Down

0 comments on commit 4e3b431

Please sign in to comment.