File tree 2 files changed +39
-1
lines changed
2 files changed +39
-1
lines changed Original file line number Diff line number Diff line change @@ -1013,9 +1013,20 @@ impl AsRef<str> for String {
1013
1013
1014
1014
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1015
1015
impl < ' a > From < & ' a str > for String {
1016
+ #[ cfg( not( test) ) ]
1016
1017
#[ inline]
1017
1018
fn from ( s : & ' a str ) -> String {
1018
- s. to_string ( )
1019
+ String { vec : <[ _ ] >:: to_vec ( s. as_bytes ( ) ) }
1020
+ }
1021
+
1022
+ // HACK(japaric): with cfg(test) the inherent `[T]::to_vec` method, which is
1023
+ // required for this method definition, is not available. Since we don't
1024
+ // require this method for testing purposes, I'll just stub it
1025
+ // NB see the slice::hack module in slice.rs for more information
1026
+ #[ inline]
1027
+ #[ cfg( test) ]
1028
+ fn from ( _: & str ) -> String {
1029
+ panic ! ( "not available with cfg(test)" ) ;
1019
1030
}
1020
1031
}
1021
1032
Original file line number Diff line number Diff line change @@ -450,3 +450,30 @@ fn bench_exact_size_shrink_to_fit(b: &mut Bencher) {
450
450
r
451
451
} ) ;
452
452
}
453
+
454
+ #[ bench]
455
+ fn bench_from_str ( b : & mut Bencher ) {
456
+ let s = "Hello there, the quick brown fox jumped over the lazy dog! \
457
+ Lorem ipsum dolor sit amet, consectetur. ";
458
+ b. iter ( || {
459
+ String :: from_str ( s)
460
+ } )
461
+ }
462
+
463
+ #[ bench]
464
+ fn bench_from ( b : & mut Bencher ) {
465
+ let s = "Hello there, the quick brown fox jumped over the lazy dog! \
466
+ Lorem ipsum dolor sit amet, consectetur. ";
467
+ b. iter ( || {
468
+ String :: from ( s)
469
+ } )
470
+ }
471
+
472
+ #[ bench]
473
+ fn bench_to_string ( b : & mut Bencher ) {
474
+ let s = "Hello there, the quick brown fox jumped over the lazy dog! \
475
+ Lorem ipsum dolor sit amet, consectetur. ";
476
+ b. iter ( || {
477
+ s. to_string ( )
478
+ } )
479
+ }
You can’t perform that action at this time.
0 commit comments