Skip to content

Commit ad7a6de

Browse files
feat!(iroh): Vec<u8> instead of static bytes for ALPNs (#2725)
Otherwise it is not possible to dynamically assign ALPN names. ## Breaking Changes - `ProtocolBuilder::accept` uses a `Vec<u8>` instead of `&'static [u8]`
1 parent ce2cfee commit ad7a6de

File tree

3 files changed

+9
-9
lines changed

3 files changed

+9
-9
lines changed

iroh/examples/custom-protocol.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ async fn main() -> Result<()> {
9595
let proto = BlobSearch::new(builder.client().blobs().clone(), builder.endpoint().clone());
9696

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

100100
match args.command {
101101
Command::Listen { text } => {

iroh/src/node/builder.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -737,7 +737,7 @@ impl<D: iroh_blobs::store::Store> ProtocolBuilder<D> {
737737
/// let handler = MyProtocol { client };
738738
///
739739
/// let node = unspawned_node
740-
/// .accept(MY_ALPN, Arc::new(handler))
740+
/// .accept(MY_ALPN.to_vec(), Arc::new(handler))
741741
/// .spawn()
742742
/// .await?;
743743
/// # node.shutdown().await?;
@@ -746,7 +746,7 @@ impl<D: iroh_blobs::store::Store> ProtocolBuilder<D> {
746746
/// ```
747747
///
748748
///
749-
pub fn accept(mut self, alpn: &'static [u8], handler: Arc<dyn ProtocolHandler>) -> Self {
749+
pub fn accept(mut self, alpn: Vec<u8>, handler: Arc<dyn ProtocolHandler>) -> Self {
750750
self.protocols.insert(alpn, handler);
751751
self
752752
}
@@ -800,15 +800,15 @@ impl<D: iroh_blobs::store::Store> ProtocolBuilder<D> {
800800
self.local_pool_handle().clone(),
801801
blob_events,
802802
);
803-
self = self.accept(iroh_blobs::protocol::ALPN, Arc::new(blobs_proto));
803+
self = self.accept(iroh_blobs::protocol::ALPN.to_vec(), Arc::new(blobs_proto));
804804

805805
// Register gossip.
806806
let gossip = self.gossip().clone();
807-
self = self.accept(GOSSIP_ALPN, Arc::new(gossip));
807+
self = self.accept(GOSSIP_ALPN.to_vec(), Arc::new(gossip));
808808

809809
// Register docs, if enabled.
810810
if let Some(docs) = self.inner.docs.clone() {
811-
self = self.accept(DOCS_ALPN, Arc::new(docs));
811+
self = self.accept(DOCS_ALPN.to_vec(), Arc::new(docs));
812812
}
813813

814814
self

iroh/src/node/protocol.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ impl<T: Send + Sync + 'static> IntoArcAny for T {
4242
}
4343

4444
#[derive(Debug, Clone, Default)]
45-
pub(super) struct ProtocolMap(BTreeMap<&'static [u8], Arc<dyn ProtocolHandler>>);
45+
pub(super) struct ProtocolMap(BTreeMap<Vec<u8>, Arc<dyn ProtocolHandler>>);
4646

4747
impl ProtocolMap {
4848
/// Returns the registered protocol handler for an ALPN as a concrete type.
@@ -59,12 +59,12 @@ impl ProtocolMap {
5959
}
6060

6161
/// Inserts a protocol handler.
62-
pub(super) fn insert(&mut self, alpn: &'static [u8], handler: Arc<dyn ProtocolHandler>) {
62+
pub(super) fn insert(&mut self, alpn: Vec<u8>, handler: Arc<dyn ProtocolHandler>) {
6363
self.0.insert(alpn, handler);
6464
}
6565

6666
/// Returns an iterator of all registered ALPN protocol identifiers.
67-
pub(super) fn alpns(&self) -> impl Iterator<Item = &&[u8]> {
67+
pub(super) fn alpns(&self) -> impl Iterator<Item = &Vec<u8>> {
6868
self.0.keys()
6969
}
7070

0 commit comments

Comments
 (0)