From cafcc05d6be23135eedc8a287847767523c1d257 Mon Sep 17 00:00:00 2001 From: Ruediger Klaehn Date: Wed, 4 Dec 2024 22:05:20 +0200 Subject: [PATCH] feat(iroh)!: remove get_protocol and the plumbing required for it it has not turned out to be as useful as originally expected --- iroh/src/protocol.rs | 44 ++------------------------------------------ 1 file changed, 2 insertions(+), 42 deletions(-) diff --git a/iroh/src/protocol.rs b/iroh/src/protocol.rs index 3c9b3d0c31..19514c3bac 100644 --- a/iroh/src/protocol.rs +++ b/iroh/src/protocol.rs @@ -40,7 +40,7 @@ //! } //! } //! ``` -use std::{any::Any, collections::BTreeMap, sync::Arc}; +use std::{collections::BTreeMap, sync::Arc}; use anyhow::Result; use futures_buffered::join_all; @@ -86,7 +86,6 @@ use crate::{endpoint::Connecting, Endpoint}; #[derive(Clone, Debug)] pub struct Router { endpoint: Endpoint, - protocols: Arc, // `Router` needs to be `Clone + Send`, and we need to `task.await` in its `shutdown()` impl. task: Arc>>>, cancel_token: CancellationToken, @@ -108,7 +107,7 @@ pub struct RouterBuilder { /// Implement this trait on a struct that should handle incoming connections. /// The protocol handler must then be registered on the node for an ALPN protocol with /// [`crate::protocol::RouterBuilder::accept`]. -pub trait ProtocolHandler: Send + Sync + IntoArcAny + std::fmt::Debug + 'static { +pub trait ProtocolHandler: Send + Sync + std::fmt::Debug + 'static { /// Handle an incoming connection. /// /// 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 } } -/// Helper trait to facilite casting from `Arc` to `Arc`. -/// -/// This trait has a blanket implementation so there is no need to implement this yourself. -pub trait IntoArcAny { - /// Casts `Arc` into `Arc`. - fn into_arc_any(self: Arc) -> Arc; -} - -impl IntoArcAny for T { - fn into_arc_any(self: Arc) -> Arc { - self - } -} - /// A typed map of protocol handlers, mapping them from ALPNs. #[derive(Debug, Clone, Default)] pub struct ProtocolMap(BTreeMap, Arc>); impl ProtocolMap { - /// Returns the registered protocol handler for an ALPN as a concrete type. - pub fn get_typed(&self, alpn: &[u8]) -> Option> { - let protocol: Arc = self.0.get(alpn)?.clone(); - let protocol_any: Arc = protocol.into_arc_any(); - let protocol_ref = Arc::downcast(protocol_any).ok()?; - Some(protocol_ref) - } - /// Returns the registered protocol handler for an ALPN as a [`Arc`]. pub fn get(&self, alpn: &[u8]) -> Option> { self.0.get(alpn).cloned() @@ -177,14 +154,6 @@ impl Router { RouterBuilder::new(endpoint) } - /// Returns a protocol handler for an ALPN. - /// - /// This downcasts to the concrete type and returns `None` if the handler registered for `alpn` - /// does not match the passed type. - pub fn get_protocol(&self, alpn: &[u8]) -> Option> { - self.protocols.get_typed(alpn) - } - /// Returns the [`Endpoint`] stored in this router. pub fn endpoint(&self) -> &Endpoint { &self.endpoint @@ -242,14 +211,6 @@ impl RouterBuilder { &self.endpoint } - /// Returns a protocol handler for an ALPN. - /// - /// This downcasts to the concrete type and returns `None` if the handler registered for `alpn` - /// does not match the passed type. - pub fn get_protocol(&self, alpn: &[u8]) -> Option> { - self.protocols.get_typed(alpn) - } - /// Spawns an accept loop and returns a handle to it encapsulated as the [`Router`]. pub async fn spawn(self) -> Result { // Update the endpoint with our alpns. @@ -335,7 +296,6 @@ impl RouterBuilder { Ok(Router { endpoint: self.endpoint, - protocols, task: Arc::new(Mutex::new(Some(task))), cancel_token: cancel, })