File tree 3 files changed +36
-0
lines changed
3 files changed +36
-0
lines changed Original file line number Diff line number Diff line change @@ -1505,6 +1505,20 @@ impl<'a> From<&'a str> for Vec<u8> {
1505
1505
// Clone-on-write
1506
1506
////////////////////////////////////////////////////////////////////////////////
1507
1507
1508
+ #[ stable( feature = "cow_from_vec" , since = "1.7.0" ) ]
1509
+ impl < ' a , T : Clone > From < & ' a [ T ] > for Cow < ' a , [ T ] > {
1510
+ fn from ( s : & ' a [ T ] ) -> Cow < ' a , [ T ] > {
1511
+ Cow :: Borrowed ( s)
1512
+ }
1513
+ }
1514
+
1515
+ #[ stable( feature = "cow_from_vec" , since = "1.7.0" ) ]
1516
+ impl < ' a , T : Clone > From < Vec < T > > for Cow < ' a , [ T ] > {
1517
+ fn from ( v : Vec < T > ) -> Cow < ' a , [ T ] > {
1518
+ Cow :: Owned ( v)
1519
+ }
1520
+ }
1521
+
1508
1522
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1509
1523
impl < ' a , T > FromIterator < T > for Cow < ' a , [ T ] > where T : Clone {
1510
1524
fn from_iter < I : IntoIterator < Item = T > > ( it : I ) -> Cow < ' a , [ T ] > {
Original file line number Diff line number Diff line change 8
8
// option. This file may not be copied, modified, or distributed
9
9
// except according to those terms.
10
10
11
+ use std:: borrow:: Cow ;
11
12
use std:: cmp:: Ordering :: { Equal , Greater , Less } ;
12
13
use std:: str:: from_utf8;
13
14
@@ -1267,6 +1268,16 @@ fn test_box_slice_clone() {
1267
1268
assert_eq ! ( data, data2) ;
1268
1269
}
1269
1270
1271
+ #[ test]
1272
+ fn test_cow_from ( ) {
1273
+ let borrowed = "borrowed" ;
1274
+ let owned = String :: from ( "owned" ) ;
1275
+ match ( Cow :: from ( owned. clone ( ) ) , Cow :: from ( borrowed) ) {
1276
+ ( Cow :: Owned ( o) , Cow :: Borrowed ( b) ) => assert ! ( o == owned && b == borrowed) ,
1277
+ _ => panic ! ( "invalid `Cow::from`" ) ,
1278
+ }
1279
+ }
1280
+
1270
1281
mod pattern {
1271
1282
use std:: str:: pattern:: Pattern ;
1272
1283
use std:: str:: pattern:: { Searcher , ReverseSearcher } ;
Original file line number Diff line number Diff line change 8
8
// option. This file may not be copied, modified, or distributed
9
9
// except according to those terms.
10
10
11
+ use std:: borrow:: Cow ;
11
12
use std:: iter:: { FromIterator , repeat} ;
12
13
use std:: mem:: size_of;
13
14
@@ -466,6 +467,16 @@ fn test_into_iter_count() {
466
467
assert_eq ! ( vec![ 1 , 2 , 3 ] . into_iter( ) . count( ) , 3 ) ;
467
468
}
468
469
470
+ #[ test]
471
+ fn test_cow_from ( ) {
472
+ let borrowed: & [ _ ] = & [ "borrowed" , "(slice)" ] ;
473
+ let owned = vec ! [ "owned" , "(vec)" ] ;
474
+ match ( Cow :: from ( owned. clone ( ) ) , Cow :: from ( borrowed) ) {
475
+ ( Cow :: Owned ( o) , Cow :: Borrowed ( b) ) => assert ! ( o == owned && b == borrowed) ,
476
+ _ => panic ! ( "invalid `Cow::from`" ) ,
477
+ }
478
+ }
479
+
469
480
#[ bench]
470
481
fn bench_new ( b : & mut Bencher ) {
471
482
b. iter ( || {
You can’t perform that action at this time.
0 commit comments