diff options
| author | bors <bors@rust-lang.org> | 2020-05-19 08:08:48 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2020-05-19 08:08:48 +0000 |
| commit | 914adf04af1c1a984707f778da3d04590c03d144 (patch) | |
| tree | cf43672b28ea604bd6b54ca7712e0001b17c5e2c /src/liballoc | |
| parent | 5943351d0eb878c1cb5af42b9e85e101d8c58ed7 (diff) | |
| parent | 22efd959109b9a231edbc81a8bc818eaa5763e78 (diff) | |
Auto merge of #71447 - cuviper:unsized_cow, r=dtolnay
impl From<Cow> for Box, Rc, and Arc These forward `Borrowed`/`Owned` values to existing `From` impls. - `Box<T>` is a fundamental type, so it would be a breaking change to add a blanket impl. Therefore, `From<Cow>` is only implemented for `[T]`, `str`, `CStr`, `OsStr`, and `Path`. - For `Rc<T>` and `Arc<T>`, `From<Cow>` is implemented for everything that implements `From` the borrowed and owned types separately.
Diffstat (limited to 'src/liballoc')
| -rw-r--r-- | src/liballoc/boxed.rs | 23 | ||||
| -rw-r--r-- | src/liballoc/rc.rs | 16 | ||||
| -rw-r--r-- | src/liballoc/sync.rs | 16 | ||||
| -rw-r--r-- | src/liballoc/tests/borrow.rs | 47 | ||||
| -rw-r--r-- | src/liballoc/tests/lib.rs | 1 |
5 files changed, 103 insertions, 0 deletions
diff --git a/src/liballoc/boxed.rs b/src/liballoc/boxed.rs index b3a771a721d..8ef6090c743 100644 --- a/src/liballoc/boxed.rs +++ b/src/liballoc/boxed.rs @@ -146,6 +146,7 @@ use core::ptr::{self, NonNull, Unique}; use core::task::{Context, Poll}; use crate::alloc::{self, AllocInit, AllocRef, Global}; +use crate::borrow::Cow; use crate::raw_vec::RawVec; use crate::str::from_boxed_utf8_unchecked; use crate::vec::Vec; @@ -800,6 +801,17 @@ impl<T: Copy> From<&[T]> for Box<[T]> { } } +#[stable(feature = "box_from_cow", since = "1.45.0")] +impl<T: Copy> From<Cow<'_, [T]>> for Box<[T]> { + #[inline] + fn from(cow: Cow<'_, [T]>) -> Box<[T]> { + match cow { + Cow::Borrowed(slice) => Box::from(slice), + Cow::Owned(slice) => Box::from(slice), + } + } +} + #[stable(feature = "box_from_slice", since = "1.17.0")] impl From<&str> for Box<str> { /// Converts a `&str` into a `Box<str>` @@ -818,6 +830,17 @@ impl From<&str> for Box<str> { } } +#[stable(feature = "box_from_cow", since = "1.45.0")] +impl From<Cow<'_, str>> for Box<str> { + #[inline] + fn from(cow: Cow<'_, str>) -> Box<str> { + match cow { + Cow::Borrowed(s) => Box::from(s), + Cow::Owned(s) => Box::from(s), + } + } +} + #[stable(feature = "boxed_str_conv", since = "1.19.0")] impl From<Box<str>> for Box<[u8]> { /// Converts a `Box<str>>` into a `Box<[u8]>` diff --git a/src/liballoc/rc.rs b/src/liballoc/rc.rs index 76213e65bab..ad96a138d7e 100644 --- a/src/liballoc/rc.rs +++ b/src/liballoc/rc.rs @@ -252,6 +252,7 @@ use core::ptr::{self, NonNull}; use core::slice::from_raw_parts_mut; use crate::alloc::{box_free, handle_alloc_error, AllocInit, AllocRef, Global, Layout}; +use crate::borrow::{Cow, ToOwned}; use crate::string::String; use crate::vec::Vec; @@ -1497,6 +1498,21 @@ impl<T> From<Vec<T>> for Rc<[T]> { } } +#[stable(feature = "shared_from_cow", since = "1.45.0")] +impl<'a, B> From<Cow<'a, B>> for Rc<B> +where + B: ToOwned + ?Sized, + Rc<B>: From<&'a B> + From<B::Owned>, +{ + #[inline] + fn from(cow: Cow<'a, B>) -> Rc<B> { + match cow { + Cow::Borrowed(s) => Rc::from(s), + Cow::Owned(s) => Rc::from(s), + } + } +} + #[stable(feature = "boxed_slice_try_from", since = "1.43.0")] impl<T, const N: usize> TryFrom<Rc<[T]>> for Rc<[T; N]> where diff --git a/src/liballoc/sync.rs b/src/liballoc/sync.rs index 2c6d130826b..b7be8042ea4 100644 --- a/src/liballoc/sync.rs +++ b/src/liballoc/sync.rs @@ -25,6 +25,7 @@ use core::sync::atomic; use core::sync::atomic::Ordering::{Acquire, Relaxed, Release, SeqCst}; use crate::alloc::{box_free, handle_alloc_error, AllocInit, AllocRef, Global, Layout}; +use crate::borrow::{Cow, ToOwned}; use crate::boxed::Box; use crate::rc::is_dangling; use crate::string::String; @@ -2128,6 +2129,21 @@ impl<T> From<Vec<T>> for Arc<[T]> { } } +#[stable(feature = "shared_from_cow", since = "1.45.0")] +impl<'a, B> From<Cow<'a, B>> for Arc<B> +where + B: ToOwned + ?Sized, + Arc<B>: From<&'a B> + From<B::Owned>, +{ + #[inline] + fn from(cow: Cow<'a, B>) -> Arc<B> { + match cow { + Cow::Borrowed(s) => Arc::from(s), + Cow::Owned(s) => Arc::from(s), + } + } +} + #[stable(feature = "boxed_slice_try_from", since = "1.43.0")] impl<T, const N: usize> TryFrom<Arc<[T]>> for Arc<[T; N]> where diff --git a/src/liballoc/tests/borrow.rs b/src/liballoc/tests/borrow.rs new file mode 100644 index 00000000000..8bfcf323f67 --- /dev/null +++ b/src/liballoc/tests/borrow.rs @@ -0,0 +1,47 @@ +use std::borrow::{Cow, ToOwned}; +use std::ffi::{CStr, OsStr}; +use std::path::Path; +use std::rc::Rc; +use std::sync::Arc; + +macro_rules! test_from_cow { + ($value:ident => $($ty:ty),+) => {$( + let borrowed = <$ty>::from(Cow::Borrowed($value)); + let owned = <$ty>::from(Cow::Owned($value.to_owned())); + assert_eq!($value, &*borrowed); + assert_eq!($value, &*owned); + )+}; + ($value:ident : & $ty:ty) => { + test_from_cow!($value => Box<$ty>, Rc<$ty>, Arc<$ty>); + } +} + +#[test] +fn test_from_cow_slice() { + let slice: &[i32] = &[1, 2, 3]; + test_from_cow!(slice: &[i32]); +} + +#[test] +fn test_from_cow_str() { + let string = "hello"; + test_from_cow!(string: &str); +} + +#[test] +fn test_from_cow_c_str() { + let string = CStr::from_bytes_with_nul(b"hello\0").unwrap(); + test_from_cow!(string: &CStr); +} + +#[test] +fn test_from_cow_os_str() { + let string = OsStr::new("hello"); + test_from_cow!(string: &OsStr); +} + +#[test] +fn test_from_cow_path() { + let path = Path::new("hello"); + test_from_cow!(path: &Path); +} diff --git a/src/liballoc/tests/lib.rs b/src/liballoc/tests/lib.rs index 78d49558262..f3da46bd0cc 100644 --- a/src/liballoc/tests/lib.rs +++ b/src/liballoc/tests/lib.rs @@ -21,6 +21,7 @@ use std::hash::{Hash, Hasher}; mod arc; mod binary_heap; +mod borrow; mod boxed; mod btree; mod cow_str; |
