@@ -27,25 +27,25 @@ pub trait Host: Unpin + 'static {
2727
2828impl Host for String {
2929 fn hostname ( & self ) -> & str {
30- self . split_once ( ':' )
30+ str_split_once ( self , ':' )
3131 . map ( |( hostname, _) | hostname)
3232 . unwrap_or ( self )
3333 }
3434
3535 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 ( ) )
3737 }
3838}
3939
4040impl Host for & ' static str {
4141 fn hostname ( & self ) -> & str {
42- self . split_once ( ':' )
42+ str_split_once ( self , ':' )
4343 . map ( |( hostname, _) | hostname)
4444 . unwrap_or ( self )
4545 }
4646
4747 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 ( ) )
4949 }
5050}
5151
@@ -69,3 +69,11 @@ mod tests {
6969 assert_connection_info_eq ! ( "example.com:false:false" , "example.com" , None ) ;
7070 }
7171}
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