@@ -163,7 +163,7 @@ impl<S: Str> StrVector for [S] {
163
163
}
164
164
}
165
165
166
- impl < S : Str > StrVector for Vec < S > {
166
+ impl < S : Str , T : AsSlice < S > > StrVector for T {
167
167
#[ inline]
168
168
fn concat ( & self ) -> String {
169
169
self . as_slice ( ) . concat ( )
@@ -929,54 +929,93 @@ mod tests {
929
929
assert_eq ! ( "ะเทศไท" , "ประเทศไทย中华Việt Nam" . slice_chars( 2 , 8 ) ) ;
930
930
}
931
931
932
- #[ test]
933
- fn test_concat ( ) {
934
- fn t ( v : & [ String ] , s : & str ) {
935
- assert_eq ! ( v. concat( ) . as_slice( ) , s) ;
932
+ struct S {
933
+ x : [ String , .. 2 ]
934
+ }
935
+
936
+ impl AsSlice < String > for S {
937
+ fn as_slice < ' a > ( & ' a self ) -> & ' a [ String ] {
938
+ & self . x
939
+ }
940
+ }
941
+
942
+ fn s ( x : & str ) -> String { x. into_string ( ) }
943
+
944
+ macro_rules! test_concat {
945
+ ( $expected: expr, $string: expr) => {
946
+ {
947
+ let s = $string. concat( ) ;
948
+ assert_eq!( $expected, s. as_slice( ) ) ;
949
+ }
936
950
}
937
- t ( & [ String :: from_str ( "you" ) , String :: from_str ( "know" ) ,
938
- String :: from_str ( "I'm" ) ,
939
- String :: from_str ( "no" ) , String :: from_str ( "good" ) ] ,
940
- "youknowI'mnogood" ) ;
941
- let v: & [ String ] = & [ ] ;
942
- t ( v, "" ) ;
943
- t ( & [ String :: from_str ( "hi" ) ] , "hi" ) ;
944
951
}
945
952
946
953
#[ test]
947
- fn test_connect ( ) {
948
- fn t ( v : & [ String ] , sep : & str , s : & str ) {
949
- assert_eq ! ( v. connect( sep) . as_slice( ) , s) ;
954
+ fn test_concat_for_different_types ( ) {
955
+ test_concat ! ( "ab" , [ "a" , "b" ] ) ;
956
+ test_concat ! ( "ab" , [ s( "a" ) , s( "b" ) ] ) ;
957
+ test_concat ! ( "ab" , vec![ "a" , "b" ] ) ;
958
+ test_concat ! ( "ab" , vec![ "a" , "b" ] . as_slice( ) ) ;
959
+ test_concat ! ( "ab" , vec![ s( "a" ) , s( "b" ) ] ) ;
960
+
961
+ let mut v0 = [ "a" , "b" ] ;
962
+ let mut v1 = [ s ( "a" ) , s ( "b" ) ] ;
963
+ unsafe {
964
+ use std:: c_vec:: CVec ;
965
+
966
+ test_concat ! ( "ab" , CVec :: new( v0. as_mut_ptr( ) , v0. len( ) ) ) ;
967
+ test_concat ! ( "ab" , CVec :: new( v1. as_mut_ptr( ) , v1. len( ) ) ) ;
950
968
}
951
- t ( & [ String :: from_str ( "you" ) , String :: from_str ( "know" ) ,
952
- String :: from_str ( "I'm" ) ,
953
- String :: from_str ( "no" ) , String :: from_str ( "good" ) ] ,
954
- " " , "you know I'm no good" ) ;
955
- let v: & [ String ] = & [ ] ;
956
- t ( v, " " , "" ) ;
957
- t ( & [ String :: from_str ( "hi" ) ] , " " , "hi" ) ;
969
+
970
+ test_concat ! ( "ab" , S { x: [ s( "a" ) , s( "b" ) ] } ) ;
958
971
}
959
972
960
973
#[ test]
961
- fn test_concat_slices ( ) {
962
- fn t ( v : & [ & str ] , s : & str ) {
963
- assert_eq ! ( v. concat( ) . as_slice( ) , s) ;
974
+ fn test_concat_for_different_lengths ( ) {
975
+ let empty: & [ & str ] = & [ ] ;
976
+ test_concat ! ( "" , empty) ;
977
+ test_concat ! ( "a" , [ "a" ] ) ;
978
+ test_concat ! ( "ab" , [ "a" , "b" ] ) ;
979
+ test_concat ! ( "abc" , [ "" , "a" , "bc" ] ) ;
980
+ }
981
+
982
+ macro_rules! test_connect {
983
+ ( $expected: expr, $string: expr, $delim: expr) => {
984
+ {
985
+ let s = $string. connect( $delim) ;
986
+ assert_eq!( $expected, s. as_slice( ) ) ;
987
+ }
964
988
}
965
- t ( & [ "you" , "know" , "I'm" , "no" , "good" ] , "youknowI'mnogood" ) ;
966
- let v: & [ & str ] = & [ ] ;
967
- t ( v, "" ) ;
968
- t ( & [ "hi" ] , "hi" ) ;
969
989
}
970
990
971
991
#[ test]
972
- fn test_connect_slices ( ) {
973
- fn t ( v : & [ & str ] , sep : & str , s : & str ) {
974
- assert_eq ! ( v. connect( sep) . as_slice( ) , s) ;
992
+ fn test_connect_for_different_types ( ) {
993
+ test_connect ! ( "a-b" , [ "a" , "b" ] , "-" ) ;
994
+ let hyphen = "-" . into_string ( ) ;
995
+ test_connect ! ( "a-b" , [ s( "a" ) , s( "b" ) ] , hyphen. as_slice( ) ) ;
996
+ test_connect ! ( "a-b" , vec![ "a" , "b" ] , hyphen. as_slice( ) ) ;
997
+ test_connect ! ( "a-b" , vec![ "a" , "b" ] . as_slice( ) , "-" ) ;
998
+ test_connect ! ( "a-b" , vec![ s( "a" ) , s( "b" ) ] , "-" ) ;
999
+
1000
+ let mut v0 = [ "a" , "b" ] ;
1001
+ let mut v1 = [ s ( "a" ) , s ( "b" ) ] ;
1002
+ unsafe {
1003
+ use std:: c_vec:: CVec ;
1004
+
1005
+ test_connect ! ( "a-b" , CVec :: new( v0. as_mut_ptr( ) , v0. len( ) ) , "-" ) ;
1006
+ test_connect ! ( "a-b" , CVec :: new( v1. as_mut_ptr( ) , v1. len( ) ) , hyphen. as_slice( ) ) ;
975
1007
}
976
- t ( & [ "you" , "know" , "I'm" , "no" , "good" ] ,
977
- " " , "you know I'm no good" ) ;
978
- t ( & [ ] , " " , "" ) ;
979
- t ( & [ "hi" ] , " " , "hi" ) ;
1008
+
1009
+ test_connect ! ( "a-b" , S { x: [ s( "a" ) , s( "b" ) ] } , "-" ) ;
1010
+ }
1011
+
1012
+ #[ test]
1013
+ fn test_connect_for_different_lengths ( ) {
1014
+ let empty: & [ & str ] = & [ ] ;
1015
+ test_connect ! ( "" , empty, "-" ) ;
1016
+ test_connect ! ( "a" , [ "a" ] , "-" ) ;
1017
+ test_connect ! ( "a-b" , [ "a" , "b" ] , "-" ) ;
1018
+ test_connect ! ( "-a-bc" , [ "" , "a" , "bc" ] , "-" ) ;
980
1019
}
981
1020
982
1021
#[ test]
0 commit comments