1818// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
1919// DEALINGS IN THE SOFTWARE.
2020
21- use crate :: upgrade:: { InboundUpgrade , OutboundUpgrade , ProtocolName , UpgradeError } ;
21+ use crate :: upgrade:: { InboundUpgrade , OutboundUpgrade , UpgradeError } ;
2222use crate :: { connection:: ConnectedPoint , Negotiated } ;
2323use futures:: { future:: Either , prelude:: * } ;
2424use log:: debug;
2525use 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 } ;
2727
2828pub ( crate ) use multistream_select:: Version ;
29- use smallvec:: SmallVec ;
30- use std:: fmt;
3129
3230// TODO: Still needed?
3331/// Applies an upgrade to the inbound and outbound direction of a connection or substream.
5553 C : AsyncRead + AsyncWrite + Unpin ,
5654 U : InboundUpgrade < Negotiated < C > > ,
5755{
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) ;
6356 InboundUpgradeApply {
6457 inner : InboundUpgradeApplyState :: Init {
65- future,
58+ future : multistream_select :: listener_select_proto ( conn , up . protocol_info ( ) . into_iter ( ) ) ,
6659 upgrade : up,
6760 } ,
6861 }
7467 C : AsyncRead + AsyncWrite + Unpin ,
7568 U : OutboundUpgrade < Negotiated < C > > ,
7669{
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) ;
8270 OutboundUpgradeApply {
8371 inner : OutboundUpgradeApplyState :: Init {
84- future,
72+ future : multistream_select :: dialer_select_proto ( conn , up . protocol_info ( ) , v ) ,
8573 upgrade : up,
8674 } ,
8775 }
@@ -96,18 +84,19 @@ where
9684 inner : InboundUpgradeApplyState < C , U > ,
9785}
9886
87+ #[ allow( clippy:: large_enum_variant) ]
9988enum InboundUpgradeApplyState < C , U >
10089where
10190 C : AsyncRead + AsyncWrite + Unpin ,
10291 U : InboundUpgrade < Negotiated < C > > ,
10392{
10493 Init {
105- future : ListenerSelectFuture < C , NameWrap < U :: Info > > ,
94+ future : ListenerSelectFuture < C , U :: Info > ,
10695 upgrade : U ,
10796 } ,
10897 Upgrade {
10998 future : Pin < Box < U :: Future > > ,
110- name : SmallVec < [ u8 ; 32 ] > ,
99+ name : String ,
111100 } ,
112101 Undefined ,
113102}
@@ -140,10 +129,9 @@ where
140129 return Poll :: Pending ;
141130 }
142131 } ;
143- let name = SmallVec :: from_slice ( info. protocol_name ( ) ) ;
144132 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 ( ) ,
147135 } ;
148136 }
149137 InboundUpgradeApplyState :: Upgrade { mut future, name } => {
@@ -153,14 +141,11 @@ where
153141 return Poll :: Pending ;
154142 }
155143 Poll :: Ready ( Ok ( x) ) => {
156- log:: trace!( "Upgraded inbound stream to {}" , DisplayProtocolName ( name ) ) ;
144+ log:: trace!( "Upgraded inbound stream to {name}" ) ;
157145 return Poll :: Ready ( Ok ( x) ) ;
158146 }
159147 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}" ) ;
164149 return Poll :: Ready ( Err ( UpgradeError :: Apply ( e) ) ) ;
165150 }
166151 }
@@ -188,12 +173,12 @@ where
188173 U : OutboundUpgrade < Negotiated < C > > ,
189174{
190175 Init {
191- future : DialerSelectFuture < C , NameWrapIter < < U :: InfoIter as IntoIterator >:: IntoIter > > ,
176+ future : DialerSelectFuture < C , < U :: InfoIter as IntoIterator >:: IntoIter > ,
192177 upgrade : U ,
193178 } ,
194179 Upgrade {
195180 future : Pin < Box < U :: Future > > ,
196- name : SmallVec < [ u8 ; 32 ] > ,
181+ name : String ,
197182 } ,
198183 Undefined ,
199184}
@@ -226,10 +211,9 @@ where
226211 return Poll :: Pending ;
227212 }
228213 } ;
229- let name = SmallVec :: from_slice ( info. protocol_name ( ) ) ;
230214 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 ( ) ,
233217 } ;
234218 }
235219 OutboundUpgradeApplyState :: Upgrade { mut future, name } => {
@@ -239,17 +223,11 @@ where
239223 return Poll :: Pending ;
240224 }
241225 Poll :: Ready ( Ok ( x) ) => {
242- log:: trace!(
243- "Upgraded outbound stream to {}" ,
244- DisplayProtocolName ( name)
245- ) ;
226+ log:: trace!( "Upgraded outbound stream to {name}" , ) ;
246227 return Poll :: Ready ( Ok ( x) ) ;
247228 }
248229 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}" , ) ;
253231 return Poll :: Ready ( Err ( UpgradeError :: Apply ( e) ) ) ;
254232 }
255233 }
@@ -261,51 +239,3 @@ where
261239 }
262240 }
263241}
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