diff options
| author | bors <bors@rust-lang.org> | 2016-02-05 06:51:05 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2016-02-05 06:51:05 +0000 |
| commit | 2ad6dc2556c19f49280b561fc1f5246ff9f9d6ed (patch) | |
| tree | 784edc64890c58f7874972f0af2dcca4818b44ae | |
| parent | dcf8ef2723d106c3467a2ee17746aa5768c3cdc6 (diff) | |
| parent | b27b8f63bc51d9aba2a1fe88cf428243d4fedfa8 (diff) | |
| download | rust-2ad6dc2556c19f49280b561fc1f5246ff9f9d6ed.tar.gz rust-2ad6dc2556c19f49280b561fc1f5246ff9f9d6ed.zip | |
Auto merge of #31386 - tbu-:pr_cow_from_vec, r=alexcrichton
Fixes #31354.
| -rw-r--r-- | src/libcollections/vec.rs | 14 | ||||
| -rw-r--r-- | src/libcollectionstest/str.rs | 11 | ||||
| -rw-r--r-- | src/libcollectionstest/vec.rs | 11 |
3 files changed, 36 insertions, 0 deletions
diff --git a/src/libcollections/vec.rs b/src/libcollections/vec.rs index a49b7304643..805d9a9807a 100644 --- a/src/libcollections/vec.rs +++ b/src/libcollections/vec.rs @@ -1505,6 +1505,20 @@ impl<'a> From<&'a str> for Vec<u8> { // Clone-on-write //////////////////////////////////////////////////////////////////////////////// +#[stable(feature = "cow_from_vec", since = "1.7.0")] +impl<'a, T: Clone> From<&'a [T]> for Cow<'a, [T]> { + fn from(s: &'a [T]) -> Cow<'a, [T]> { + Cow::Borrowed(s) + } +} + +#[stable(feature = "cow_from_vec", since = "1.7.0")] +impl<'a, T: Clone> From<Vec<T>> for Cow<'a, [T]> { + fn from(v: Vec<T>) -> Cow<'a, [T]> { + Cow::Owned(v) + } +} + #[stable(feature = "rust1", since = "1.0.0")] impl<'a, T> FromIterator<T> for Cow<'a, [T]> where T: Clone { fn from_iter<I: IntoIterator<Item = T>>(it: I) -> Cow<'a, [T]> { diff --git a/src/libcollectionstest/str.rs b/src/libcollectionstest/str.rs index 0fde70aacdc..25457043a9d 100644 --- a/src/libcollectionstest/str.rs +++ b/src/libcollectionstest/str.rs @@ -8,6 +8,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. +use std::borrow::Cow; use std::cmp::Ordering::{Equal, Greater, Less}; use std::str::from_utf8; @@ -1267,6 +1268,16 @@ fn test_box_slice_clone() { assert_eq!(data, data2); } +#[test] +fn test_cow_from() { + let borrowed = "borrowed"; + let owned = String::from("owned"); + match (Cow::from(owned.clone()), Cow::from(borrowed)) { + (Cow::Owned(o), Cow::Borrowed(b)) => assert!(o == owned && b == borrowed), + _ => panic!("invalid `Cow::from`"), + } +} + mod pattern { use std::str::pattern::Pattern; use std::str::pattern::{Searcher, ReverseSearcher}; diff --git a/src/libcollectionstest/vec.rs b/src/libcollectionstest/vec.rs index b799be218e6..6a47f16c5ca 100644 --- a/src/libcollectionstest/vec.rs +++ b/src/libcollectionstest/vec.rs @@ -8,6 +8,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. +use std::borrow::Cow; use std::iter::{FromIterator, repeat}; use std::mem::size_of; @@ -466,6 +467,16 @@ fn test_into_iter_count() { assert_eq!(vec![1, 2, 3].into_iter().count(), 3); } +#[test] +fn test_cow_from() { + let borrowed: &[_] = &["borrowed", "(slice)"]; + let owned = vec!["owned", "(vec)"]; + match (Cow::from(owned.clone()), Cow::from(borrowed)) { + (Cow::Owned(o), Cow::Borrowed(b)) => assert!(o == owned && b == borrowed), + _ => panic!("invalid `Cow::from`"), + } +} + #[bench] fn bench_new(b: &mut Bencher) { b.iter(|| { |
