Skip to content

Commit 2ad6dc2

Browse files
committedFeb 5, 2016
Auto merge of #31386 - tbu-:pr_cow_from_vec, r=alexcrichton
Fixes #31354.
2 parents dcf8ef2 + b27b8f6 commit 2ad6dc2

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed
 

‎src/libcollections/vec.rs

+14
Original file line numberDiff line numberDiff 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")]
15091523
impl<'a, T> FromIterator<T> for Cow<'a, [T]> where T: Clone {
15101524
fn from_iter<I: IntoIterator<Item = T>>(it: I) -> Cow<'a, [T]> {

‎src/libcollectionstest/str.rs

+11
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
use std::borrow::Cow;
1112
use std::cmp::Ordering::{Equal, Greater, Less};
1213
use 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+
12701281
mod pattern {
12711282
use std::str::pattern::Pattern;
12721283
use std::str::pattern::{Searcher, ReverseSearcher};

‎src/libcollectionstest/vec.rs

+11
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
use std::borrow::Cow;
1112
use std::iter::{FromIterator, repeat};
1213
use 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]
470481
fn bench_new(b: &mut Bencher) {
471482
b.iter(|| {

0 commit comments

Comments
 (0)
Please sign in to comment.