File tree Expand file tree Collapse file tree 3 files changed +36
-0
lines changed Expand file tree Collapse file tree 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> {
15051505// Clone-on-write
15061506////////////////////////////////////////////////////////////////////////////////
15071507
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+
15081522#[ stable( feature = "rust1" , since = "1.0.0" ) ]
15091523impl < ' a , T > FromIterator < T > for Cow < ' a , [ T ] > where T : Clone {
15101524 fn from_iter < I : IntoIterator < Item = T > > ( it : I ) -> Cow < ' a , [ T ] > {
Original file line number Diff line number Diff line change 88// option. This file may not be copied, modified, or distributed
99// except according to those terms.
1010
11+ use std:: borrow:: Cow ;
1112use std:: cmp:: Ordering :: { Equal , Greater , Less } ;
1213use std:: str:: from_utf8;
1314
@@ -1267,6 +1268,16 @@ fn test_box_slice_clone() {
12671268 assert_eq ! ( data, data2) ;
12681269}
12691270
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+
12701281mod pattern {
12711282 use std:: str:: pattern:: Pattern ;
12721283 use std:: str:: pattern:: { Searcher , ReverseSearcher } ;
Original file line number Diff line number Diff line change 88// option. This file may not be copied, modified, or distributed
99// except according to those terms.
1010
11+ use std:: borrow:: Cow ;
1112use std:: iter:: { FromIterator , repeat} ;
1213use std:: mem:: size_of;
1314
@@ -466,6 +467,16 @@ fn test_into_iter_count() {
466467 assert_eq ! ( vec![ 1 , 2 , 3 ] . into_iter( ) . count( ) , 3 ) ;
467468}
468469
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+
469480#[ bench]
470481fn bench_new ( b : & mut Bencher ) {
471482 b. iter ( || {
You can’t perform that action at this time.
0 commit comments