40
40
//! }
41
41
//! }
42
42
//! ```
43
- use std:: { any :: Any , collections:: BTreeMap , sync:: Arc } ;
43
+ use std:: { collections:: BTreeMap , sync:: Arc } ;
44
44
45
45
use anyhow:: Result ;
46
46
use futures_buffered:: join_all;
@@ -86,7 +86,6 @@ use crate::{endpoint::Connecting, Endpoint};
86
86
#[ derive( Clone , Debug ) ]
87
87
pub struct Router {
88
88
endpoint : Endpoint ,
89
- protocols : Arc < ProtocolMap > ,
90
89
// `Router` needs to be `Clone + Send`, and we need to `task.await` in its `shutdown()` impl.
91
90
task : Arc < Mutex < Option < AbortOnDropHandle < ( ) > > > > ,
92
91
cancel_token : CancellationToken ,
@@ -108,7 +107,7 @@ pub struct RouterBuilder {
108
107
/// Implement this trait on a struct that should handle incoming connections.
109
108
/// The protocol handler must then be registered on the node for an ALPN protocol with
110
109
/// [`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 {
112
111
/// Handle an incoming connection.
113
112
///
114
113
/// 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
120
119
}
121
120
}
122
121
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
-
137
122
/// A typed map of protocol handlers, mapping them from ALPNs.
138
123
#[ derive( Debug , Clone , Default ) ]
139
124
pub struct ProtocolMap ( BTreeMap < Vec < u8 > , Arc < dyn ProtocolHandler > > ) ;
140
125
141
126
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
-
150
127
/// Returns the registered protocol handler for an ALPN as a [`Arc<dyn ProtocolHandler>`].
151
128
pub fn get ( & self , alpn : & [ u8 ] ) -> Option < Arc < dyn ProtocolHandler > > {
152
129
self . 0 . get ( alpn) . cloned ( )
@@ -177,14 +154,6 @@ impl Router {
177
154
RouterBuilder :: new ( endpoint)
178
155
}
179
156
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
-
188
157
/// Returns the [`Endpoint`] stored in this router.
189
158
pub fn endpoint ( & self ) -> & Endpoint {
190
159
& self . endpoint
@@ -242,14 +211,6 @@ impl RouterBuilder {
242
211
& self . endpoint
243
212
}
244
213
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
-
253
214
/// Spawns an accept loop and returns a handle to it encapsulated as the [`Router`].
254
215
pub async fn spawn ( self ) -> Result < Router > {
255
216
// Update the endpoint with our alpns.
@@ -267,7 +228,6 @@ impl RouterBuilder {
267
228
268
229
let mut join_set = JoinSet :: new ( ) ;
269
230
let endpoint = self . endpoint . clone ( ) ;
270
- let protos = protocols. clone ( ) ;
271
231
272
232
// We use a child token of the endpoint, to ensure that this is shutdown
273
233
// when the endpoint is shutdown, but that we can shutdown ourselves independently.
@@ -278,7 +238,6 @@ impl RouterBuilder {
278
238
// Make sure to cancel the token, if this future ever exits.
279
239
let _cancel_guard = cancel_token. clone ( ) . drop_guard ( ) ;
280
240
281
- let protocols = protos;
282
241
loop {
283
242
tokio:: select! {
284
243
biased;
@@ -335,7 +294,6 @@ impl RouterBuilder {
335
294
336
295
Ok ( Router {
337
296
endpoint : self . endpoint ,
338
- protocols,
339
297
task : Arc :: new ( Mutex :: new ( Some ( task) ) ) ,
340
298
cancel_token : cancel,
341
299
} )
0 commit comments