@@ -27,25 +27,25 @@ pub trait Host: Unpin + 'static {
27
27
28
28
impl Host for String {
29
29
fn hostname ( & self ) -> & str {
30
- self . split_once ( ':' )
30
+ str_split_once ( self , ':' )
31
31
. map ( |( hostname, _) | hostname)
32
32
. unwrap_or ( self )
33
33
}
34
34
35
35
fn port ( & self ) -> Option < u16 > {
36
- self . split_once ( ':' ) . and_then ( |( _, port) | port. parse ( ) . ok ( ) )
36
+ str_split_once ( self , ':' ) . and_then ( |( _, port) | port. parse ( ) . ok ( ) )
37
37
}
38
38
}
39
39
40
40
impl Host for & ' static str {
41
41
fn hostname ( & self ) -> & str {
42
- self . split_once ( ':' )
42
+ str_split_once ( self , ':' )
43
43
. map ( |( hostname, _) | hostname)
44
44
. unwrap_or ( self )
45
45
}
46
46
47
47
fn port ( & self ) -> Option < u16 > {
48
- self . split_once ( ':' ) . and_then ( |( _, port) | port. parse ( ) . ok ( ) )
48
+ str_split_once ( self , ':' ) . and_then ( |( _, port) | port. parse ( ) . ok ( ) )
49
49
}
50
50
}
51
51
@@ -69,3 +69,11 @@ mod tests {
69
69
assert_connection_info_eq ! ( "example.com:false:false" , "example.com" , None ) ;
70
70
}
71
71
}
72
+
73
+ // `str::split_once` is stabilized in 1.52.0
74
+ fn str_split_once ( str : & str , delimiter : char ) -> Option < ( & str , & str ) > {
75
+ let mut splitn = str. splitn ( 2 , delimiter) ;
76
+ let prefix = splitn. next ( ) ?;
77
+ let suffix = splitn. next ( ) ?;
78
+ Some ( ( prefix, suffix) )
79
+ }
0 commit comments