Skip to content

Commit 1323c9a

Browse files
authored
feat(iroh)!: remove get_protocol and the plumbing required for it (#3009)
## Description Remove get_protocol and the plumbing required for it it has not turned out to be as useful as originally expected ## Breaking Changes - iroh::protocol::Router::get_protocol is removed - iroh::protocol::RouterBuilder::get_protocol is removed - iroh::protocol::ProtocolMap::get_typed is removed - iroh::protocol::IntoArcAny is removed ## Notes & open questions ## 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 b76500d commit 1323c9a

File tree

1 file changed

+2
-44
lines changed

1 file changed

+2
-44
lines changed

iroh/src/protocol.rs

+2-44
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
//! }
4141
//! }
4242
//! ```
43-
use std::{any::Any, collections::BTreeMap, sync::Arc};
43+
use std::{collections::BTreeMap, sync::Arc};
4444

4545
use anyhow::Result;
4646
use futures_buffered::join_all;
@@ -86,7 +86,6 @@ use crate::{endpoint::Connecting, Endpoint};
8686
#[derive(Clone, Debug)]
8787
pub struct Router {
8888
endpoint: Endpoint,
89-
protocols: Arc<ProtocolMap>,
9089
// `Router` needs to be `Clone + Send`, and we need to `task.await` in its `shutdown()` impl.
9190
task: Arc<Mutex<Option<AbortOnDropHandle<()>>>>,
9291
cancel_token: CancellationToken,
@@ -108,7 +107,7 @@ pub struct RouterBuilder {
108107
/// Implement this trait on a struct that should handle incoming connections.
109108
/// The protocol handler must then be registered on the node for an ALPN protocol with
110109
/// [`crate::protocol::RouterBuilder::accept`].
111-
pub trait ProtocolHandler: Send + Sync + IntoArcAny + std::fmt::Debug + 'static {
110+
pub trait ProtocolHandler: Send + Sync + std::fmt::Debug + 'static {
112111
/// Handle an incoming connection.
113112
///
114113
/// This runs on a freshly spawned tokio task so this can be long-running.
@@ -120,33 +119,11 @@ pub trait ProtocolHandler: Send + Sync + IntoArcAny + std::fmt::Debug + 'static
120119
}
121120
}
122121

123-
/// Helper trait to facilite casting from `Arc<dyn T>` to `Arc<dyn Any>`.
124-
///
125-
/// This trait has a blanket implementation so there is no need to implement this yourself.
126-
pub trait IntoArcAny {
127-
/// Casts `Arc<Self>` into `Arc<dyn Any + Send + Sync>`.
128-
fn into_arc_any(self: Arc<Self>) -> Arc<dyn Any + Send + Sync>;
129-
}
130-
131-
impl<T: Send + Sync + 'static> IntoArcAny for T {
132-
fn into_arc_any(self: Arc<Self>) -> Arc<dyn Any + Send + Sync> {
133-
self
134-
}
135-
}
136-
137122
/// A typed map of protocol handlers, mapping them from ALPNs.
138123
#[derive(Debug, Clone, Default)]
139124
pub struct ProtocolMap(BTreeMap<Vec<u8>, Arc<dyn ProtocolHandler>>);
140125

141126
impl ProtocolMap {
142-
/// Returns the registered protocol handler for an ALPN as a concrete type.
143-
pub fn get_typed<P: ProtocolHandler>(&self, alpn: &[u8]) -> Option<Arc<P>> {
144-
let protocol: Arc<dyn ProtocolHandler> = self.0.get(alpn)?.clone();
145-
let protocol_any: Arc<dyn Any + Send + Sync> = protocol.into_arc_any();
146-
let protocol_ref = Arc::downcast(protocol_any).ok()?;
147-
Some(protocol_ref)
148-
}
149-
150127
/// Returns the registered protocol handler for an ALPN as a [`Arc<dyn ProtocolHandler>`].
151128
pub fn get(&self, alpn: &[u8]) -> Option<Arc<dyn ProtocolHandler>> {
152129
self.0.get(alpn).cloned()
@@ -177,14 +154,6 @@ impl Router {
177154
RouterBuilder::new(endpoint)
178155
}
179156

180-
/// Returns a protocol handler for an ALPN.
181-
///
182-
/// This downcasts to the concrete type and returns `None` if the handler registered for `alpn`
183-
/// does not match the passed type.
184-
pub fn get_protocol<P: ProtocolHandler>(&self, alpn: &[u8]) -> Option<Arc<P>> {
185-
self.protocols.get_typed(alpn)
186-
}
187-
188157
/// Returns the [`Endpoint`] stored in this router.
189158
pub fn endpoint(&self) -> &Endpoint {
190159
&self.endpoint
@@ -242,14 +211,6 @@ impl RouterBuilder {
242211
&self.endpoint
243212
}
244213

245-
/// Returns a protocol handler for an ALPN.
246-
///
247-
/// This downcasts to the concrete type and returns `None` if the handler registered for `alpn`
248-
/// does not match the passed type.
249-
pub fn get_protocol<P: ProtocolHandler>(&self, alpn: &[u8]) -> Option<Arc<P>> {
250-
self.protocols.get_typed(alpn)
251-
}
252-
253214
/// Spawns an accept loop and returns a handle to it encapsulated as the [`Router`].
254215
pub async fn spawn(self) -> Result<Router> {
255216
// Update the endpoint with our alpns.
@@ -267,7 +228,6 @@ impl RouterBuilder {
267228

268229
let mut join_set = JoinSet::new();
269230
let endpoint = self.endpoint.clone();
270-
let protos = protocols.clone();
271231

272232
// We use a child token of the endpoint, to ensure that this is shutdown
273233
// when the endpoint is shutdown, but that we can shutdown ourselves independently.
@@ -278,7 +238,6 @@ impl RouterBuilder {
278238
// Make sure to cancel the token, if this future ever exits.
279239
let _cancel_guard = cancel_token.clone().drop_guard();
280240

281-
let protocols = protos;
282241
loop {
283242
tokio::select! {
284243
biased;
@@ -335,7 +294,6 @@ impl RouterBuilder {
335294

336295
Ok(Router {
337296
endpoint: self.endpoint,
338-
protocols,
339297
task: Arc::new(Mutex::new(Some(task))),
340298
cancel_token: cancel,
341299
})

0 commit comments

Comments
 (0)