File tree Expand file tree Collapse file tree 2 files changed +39
-1
lines changed Expand file tree Collapse file tree 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 {
10131013
10141014#[ stable( feature = "rust1" , since = "1.0.0" ) ]
10151015impl < ' a > From < & ' a str > for String {
1016+ #[ cfg( not( test) ) ]
10161017 #[ inline]
10171018 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)" ) ;
10191030 }
10201031}
10211032
Original file line number Diff line number Diff line change @@ -450,3 +450,30 @@ fn bench_exact_size_shrink_to_fit(b: &mut Bencher) {
450450 r
451451 } ) ;
452452}
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