18
18
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
19
19
// DEALINGS IN THE SOFTWARE.
20
20
21
- use crate :: upgrade:: { InboundUpgrade , OutboundUpgrade , ProtocolName , UpgradeError } ;
21
+ use crate :: upgrade:: { InboundUpgrade , OutboundUpgrade , UpgradeError } ;
22
22
use crate :: { connection:: ConnectedPoint , Negotiated } ;
23
23
use futures:: { future:: Either , prelude:: * } ;
24
24
use log:: debug;
25
25
use multistream_select:: { self , DialerSelectFuture , ListenerSelectFuture } ;
26
- use std:: { iter , mem, pin:: Pin , task:: Context , task:: Poll } ;
26
+ use std:: { mem, pin:: Pin , task:: Context , task:: Poll } ;
27
27
28
28
pub ( crate ) use multistream_select:: Version ;
29
- use smallvec:: SmallVec ;
30
- use std:: fmt;
31
29
32
30
// TODO: Still needed?
33
31
/// Applies an upgrade to the inbound and outbound direction of a connection or substream.
55
53
C : AsyncRead + AsyncWrite + Unpin ,
56
54
U : InboundUpgrade < Negotiated < C > > ,
57
55
{
58
- let iter = up
59
- . protocol_info ( )
60
- . into_iter ( )
61
- . map ( NameWrap as fn ( _) -> NameWrap < _ > ) ;
62
- let future = multistream_select:: listener_select_proto ( conn, iter) ;
63
56
InboundUpgradeApply {
64
57
inner : InboundUpgradeApplyState :: Init {
65
- future,
58
+ future : multistream_select :: listener_select_proto ( conn , up . protocol_info ( ) . into_iter ( ) ) ,
66
59
upgrade : up,
67
60
} ,
68
61
}
74
67
C : AsyncRead + AsyncWrite + Unpin ,
75
68
U : OutboundUpgrade < Negotiated < C > > ,
76
69
{
77
- let iter = up
78
- . protocol_info ( )
79
- . into_iter ( )
80
- . map ( NameWrap as fn ( _) -> NameWrap < _ > ) ;
81
- let future = multistream_select:: dialer_select_proto ( conn, iter, v) ;
82
70
OutboundUpgradeApply {
83
71
inner : OutboundUpgradeApplyState :: Init {
84
- future,
72
+ future : multistream_select :: dialer_select_proto ( conn , up . protocol_info ( ) , v ) ,
85
73
upgrade : up,
86
74
} ,
87
75
}
@@ -96,18 +84,19 @@ where
96
84
inner : InboundUpgradeApplyState < C , U > ,
97
85
}
98
86
87
+ #[ allow( clippy:: large_enum_variant) ]
99
88
enum InboundUpgradeApplyState < C , U >
100
89
where
101
90
C : AsyncRead + AsyncWrite + Unpin ,
102
91
U : InboundUpgrade < Negotiated < C > > ,
103
92
{
104
93
Init {
105
- future : ListenerSelectFuture < C , NameWrap < U :: Info > > ,
94
+ future : ListenerSelectFuture < C , U :: Info > ,
106
95
upgrade : U ,
107
96
} ,
108
97
Upgrade {
109
98
future : Pin < Box < U :: Future > > ,
110
- name : SmallVec < [ u8 ; 32 ] > ,
99
+ name : String ,
111
100
} ,
112
101
Undefined ,
113
102
}
@@ -140,10 +129,9 @@ where
140
129
return Poll :: Pending ;
141
130
}
142
131
} ;
143
- let name = SmallVec :: from_slice ( info. protocol_name ( ) ) ;
144
132
self . inner = InboundUpgradeApplyState :: Upgrade {
145
- future : Box :: pin ( upgrade. upgrade_inbound ( io, info. 0 ) ) ,
146
- name,
133
+ future : Box :: pin ( upgrade. upgrade_inbound ( io, info. clone ( ) ) ) ,
134
+ name : info . as_ref ( ) . to_owned ( ) ,
147
135
} ;
148
136
}
149
137
InboundUpgradeApplyState :: Upgrade { mut future, name } => {
@@ -153,14 +141,11 @@ where
153
141
return Poll :: Pending ;
154
142
}
155
143
Poll :: Ready ( Ok ( x) ) => {
156
- log:: trace!( "Upgraded inbound stream to {}" , DisplayProtocolName ( name ) ) ;
144
+ log:: trace!( "Upgraded inbound stream to {name}" ) ;
157
145
return Poll :: Ready ( Ok ( x) ) ;
158
146
}
159
147
Poll :: Ready ( Err ( e) ) => {
160
- debug ! (
161
- "Failed to upgrade inbound stream to {}" ,
162
- DisplayProtocolName ( name)
163
- ) ;
148
+ debug ! ( "Failed to upgrade inbound stream to {name}" ) ;
164
149
return Poll :: Ready ( Err ( UpgradeError :: Apply ( e) ) ) ;
165
150
}
166
151
}
@@ -188,12 +173,12 @@ where
188
173
U : OutboundUpgrade < Negotiated < C > > ,
189
174
{
190
175
Init {
191
- future : DialerSelectFuture < C , NameWrapIter < < U :: InfoIter as IntoIterator >:: IntoIter > > ,
176
+ future : DialerSelectFuture < C , < U :: InfoIter as IntoIterator >:: IntoIter > ,
192
177
upgrade : U ,
193
178
} ,
194
179
Upgrade {
195
180
future : Pin < Box < U :: Future > > ,
196
- name : SmallVec < [ u8 ; 32 ] > ,
181
+ name : String ,
197
182
} ,
198
183
Undefined ,
199
184
}
@@ -226,10 +211,9 @@ where
226
211
return Poll :: Pending ;
227
212
}
228
213
} ;
229
- let name = SmallVec :: from_slice ( info. protocol_name ( ) ) ;
230
214
self . inner = OutboundUpgradeApplyState :: Upgrade {
231
- future : Box :: pin ( upgrade. upgrade_outbound ( connection, info. 0 ) ) ,
232
- name,
215
+ future : Box :: pin ( upgrade. upgrade_outbound ( connection, info. clone ( ) ) ) ,
216
+ name : info . as_ref ( ) . to_owned ( ) ,
233
217
} ;
234
218
}
235
219
OutboundUpgradeApplyState :: Upgrade { mut future, name } => {
@@ -239,17 +223,11 @@ where
239
223
return Poll :: Pending ;
240
224
}
241
225
Poll :: Ready ( Ok ( x) ) => {
242
- log:: trace!(
243
- "Upgraded outbound stream to {}" ,
244
- DisplayProtocolName ( name)
245
- ) ;
226
+ log:: trace!( "Upgraded outbound stream to {name}" , ) ;
246
227
return Poll :: Ready ( Ok ( x) ) ;
247
228
}
248
229
Poll :: Ready ( Err ( e) ) => {
249
- debug ! (
250
- "Failed to upgrade outbound stream to {}" ,
251
- DisplayProtocolName ( name)
252
- ) ;
230
+ debug ! ( "Failed to upgrade outbound stream to {name}" , ) ;
253
231
return Poll :: Ready ( Err ( UpgradeError :: Apply ( e) ) ) ;
254
232
}
255
233
}
@@ -261,51 +239,3 @@ where
261
239
}
262
240
}
263
241
}
264
-
265
- type NameWrapIter < I > = iter:: Map < I , fn ( <I as Iterator >:: Item ) -> NameWrap < <I as Iterator >:: Item > > ;
266
-
267
- /// Wrapper type to expose an `AsRef<[u8]>` impl for all types implementing `ProtocolName`.
268
- #[ derive( Clone ) ]
269
- struct NameWrap < N > ( N ) ;
270
-
271
- impl < N : ProtocolName > AsRef < [ u8 ] > for NameWrap < N > {
272
- fn as_ref ( & self ) -> & [ u8 ] {
273
- self . 0 . protocol_name ( )
274
- }
275
- }
276
-
277
- /// Wrapper for printing a [`ProtocolName`] that is expected to be mostly ASCII
278
- pub ( crate ) struct DisplayProtocolName < N > ( pub ( crate ) N ) ;
279
-
280
- impl < N : ProtocolName > fmt:: Display for DisplayProtocolName < N > {
281
- fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
282
- use fmt:: Write ;
283
- for byte in self . 0 . protocol_name ( ) {
284
- if ( b' ' ..=b'~' ) . contains ( byte) {
285
- f. write_char ( char:: from ( * byte) ) ?;
286
- } else {
287
- write ! ( f, "<{byte:02X}>" ) ?;
288
- }
289
- }
290
- Ok ( ( ) )
291
- }
292
- }
293
-
294
- #[ cfg( test) ]
295
- mod tests {
296
- use super :: * ;
297
-
298
- #[ test]
299
- fn display_protocol_name ( ) {
300
- assert_eq ! ( DisplayProtocolName ( b"/hello/1.0" ) . to_string( ) , "/hello/1.0" ) ;
301
- assert_eq ! ( DisplayProtocolName ( "/hellö/" ) . to_string( ) , "/hell<C3><B6>/" ) ;
302
- assert_eq ! (
303
- DisplayProtocolName ( ( 0u8 ..=255 ) . collect:: <Vec <_>>( ) ) . to_string( ) ,
304
- ( 0 ..32 )
305
- . map( |c| format!( "<{c:02X}>" ) )
306
- . chain( ( 32 ..127 ) . map( |c| format!( "{}" , char :: from_u32( c) . unwrap( ) ) ) )
307
- . chain( ( 127 ..256 ) . map( |c| format!( "<{c:02X}>" ) ) )
308
- . collect:: <String >( )
309
- ) ;
310
- }
311
- }
0 commit comments