From b0fb57bd8d18946787a79d1244154f2512dcf15b Mon Sep 17 00:00:00 2001 From: Josh Stone Date: Wed, 22 Apr 2020 13:03:05 -0700 Subject: [PATCH 1/4] impl From for boxed slices and strings These forward `Borrowed`/`Owned` values to existing `Box::from` impls. - `From> for Box<[T]>` - `From> for Box` - `From> for Box` - `From> for Box` - `From> for Box` --- src/liballoc/boxed.rs | 23 +++++++++++++++++++++++ src/libstd/ffi/c_str.rs | 11 +++++++++++ src/libstd/ffi/os_str.rs | 11 +++++++++++ src/libstd/path.rs | 11 +++++++++++ 4 files changed, 56 insertions(+) diff --git a/src/liballoc/boxed.rs b/src/liballoc/boxed.rs index db7420954a0..de906c7f7b0 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; @@ -774,6 +775,17 @@ impl From<&[T]> for Box<[T]> { } } +#[stable(feature = "box_from_cow", since = "1.45.0")] +impl From> 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 { /// Converts a `&str` into a `Box` @@ -792,6 +804,17 @@ impl From<&str> for Box { } } +#[stable(feature = "box_from_cow", since = "1.45.0")] +impl From> for Box { + #[inline] + fn from(cow: Cow<'_, str>) -> Box { + 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> for Box<[u8]> { /// Converts a `Box>` into a `Box<[u8]>` diff --git a/src/libstd/ffi/c_str.rs b/src/libstd/ffi/c_str.rs index 0a4802fb2c8..4bac9a4917d 100644 --- a/src/libstd/ffi/c_str.rs +++ b/src/libstd/ffi/c_str.rs @@ -730,6 +730,17 @@ impl From<&CStr> for Box { } } +#[stable(feature = "box_from_cow", since = "1.45.0")] +impl From> for Box { + #[inline] + fn from(cow: Cow<'_, CStr>) -> Box { + match cow { + Cow::Borrowed(s) => Box::from(s), + Cow::Owned(s) => Box::from(s), + } + } +} + #[stable(feature = "c_string_from_box", since = "1.18.0")] impl From> for CString { /// Converts a [`Box`]`` into a [`CString`] without copying or allocating. diff --git a/src/libstd/ffi/os_str.rs b/src/libstd/ffi/os_str.rs index 4fde3316973..7a2d34968e0 100644 --- a/src/libstd/ffi/os_str.rs +++ b/src/libstd/ffi/os_str.rs @@ -849,6 +849,17 @@ impl From<&OsStr> for Box { } } +#[stable(feature = "box_from_cow", since = "1.45.0")] +impl From> for Box { + #[inline] + fn from(cow: Cow<'_, OsStr>) -> Box { + match cow { + Cow::Borrowed(s) => Box::from(s), + Cow::Owned(s) => Box::from(s), + } + } +} + #[stable(feature = "os_string_from_box", since = "1.18.0")] impl From> for OsString { /// Converts a [`Box`]`<`[`OsStr`]`>` into a `OsString` without copying or diff --git a/src/libstd/path.rs b/src/libstd/path.rs index 173d6d1cfa7..ed1dc787329 100644 --- a/src/libstd/path.rs +++ b/src/libstd/path.rs @@ -1433,6 +1433,17 @@ impl From<&Path> for Box { } } +#[stable(feature = "box_from_cow", since = "1.45.0")] +impl From> for Box { + #[inline] + fn from(cow: Cow<'_, Path>) -> Box { + match cow { + Cow::Borrowed(path) => Box::from(path), + Cow::Owned(path) => Box::from(path), + } + } +} + #[stable(feature = "path_buf_from_box", since = "1.18.0")] impl From> for PathBuf { /// Converts a `Box` into a `PathBuf` From 6d40751b37ef453bee5d8a9c47b00ef205d61738 Mon Sep 17 00:00:00 2001 From: Josh Stone Date: Wed, 22 Apr 2020 13:33:42 -0700 Subject: [PATCH 2/4] impl From for Rc and Arc These forward `Borrowed`/`Owned` values to existing `From` impls. impl<'a, B> From> for Rc where B: ToOwned + ?Sized, Rc: From<&'a B> + From, impl<'a, B> From> for Arc where B: ToOwned + ?Sized, Arc: From<&'a B> + From, --- src/liballoc/rc.rs | 16 ++++++++++++++++ src/liballoc/sync.rs | 16 ++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/src/liballoc/rc.rs b/src/liballoc/rc.rs index e106b4354e4..44a390c3ec3 100644 --- a/src/liballoc/rc.rs +++ b/src/liballoc/rc.rs @@ -252,6 +252,7 @@ use core::ptr::{self, NonNull}; use core::slice::{self, 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; @@ -1492,6 +1493,21 @@ impl From> for Rc<[T]> { } } +#[stable(feature = "shared_from_cow", since = "1.45.0")] +impl<'a, B> From> for Rc +where + B: ToOwned + ?Sized, + Rc: From<&'a B> + From, +{ + #[inline] + fn from(cow: Cow<'a, B>) -> Rc { + 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 TryFrom> for Rc<[T; N]> where diff --git a/src/liballoc/sync.rs b/src/liballoc/sync.rs index 54df2b60857..a45929e86fc 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; @@ -2047,6 +2048,21 @@ impl From> for Arc<[T]> { } } +#[stable(feature = "shared_from_cow", since = "1.45.0")] +impl<'a, B> From> for Arc +where + B: ToOwned + ?Sized, + Arc: From<&'a B> + From, +{ + #[inline] + fn from(cow: Cow<'a, B>) -> Arc { + 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 TryFrom> for Arc<[T; N]> where From 23f71fe5b518c6136990f79733155b4fa1314d2b Mon Sep 17 00:00:00 2001 From: Josh Stone Date: Wed, 22 Apr 2020 14:16:21 -0700 Subject: [PATCH 3/4] Add tests from Cow --- src/liballoc/tests/borrow.rs | 47 ++++++++++++++++++++++++++++++++++++ src/liballoc/tests/lib.rs | 1 + 2 files changed, 48 insertions(+) create mode 100644 src/liballoc/tests/borrow.rs 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 ad6feaeebc6..cbeceec6d78 100644 --- a/src/liballoc/tests/lib.rs +++ b/src/liballoc/tests/lib.rs @@ -20,6 +20,7 @@ use std::hash::{Hash, Hasher}; mod arc; mod binary_heap; +mod borrow; mod boxed; mod btree; mod cow_str; From 22efd959109b9a231edbc81a8bc818eaa5763e78 Mon Sep 17 00:00:00 2001 From: Josh Stone Date: Wed, 22 Apr 2020 14:40:27 -0700 Subject: [PATCH 4/4] Bless From UI changes --- .../array-impls/alloc-types-no-impls-length-33.stderr | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/test/ui/const-generics/array-impls/alloc-types-no-impls-length-33.stderr b/src/test/ui/const-generics/array-impls/alloc-types-no-impls-length-33.stderr index d795840551c..3a3f6e3d420 100644 --- a/src/test/ui/const-generics/array-impls/alloc-types-no-impls-length-33.stderr +++ b/src/test/ui/const-generics/array-impls/alloc-types-no-impls-length-33.stderr @@ -18,7 +18,7 @@ LL | let boxed_array = >::try_from(boxed_slice); as std::convert::From<&str>> as std::convert::From>> as std::convert::From> - and 16 others + and 21 others = note: required because of the requirements on the impl of `std::convert::Into>` for `std::boxed::Box<[i32]>` = note: required because of the requirements on the impl of `std::convert::TryFrom>` for `std::boxed::Box<[i32; 33]>` @@ -38,11 +38,11 @@ LL | let boxed_array = >::try_from(boxed_slice); | ^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `std::convert::From>` is not implemented for `std::rc::Rc<[i32; 33]>` | = help: the following implementations were found: + as std::convert::From>> as std::convert::From> as std::convert::From>> as std::convert::From<&[T]>> - as std::convert::From>> - and 8 others + and 9 others = note: required because of the requirements on the impl of `std::convert::Into>` for `std::rc::Rc<[i32]>` = note: required because of the requirements on the impl of `std::convert::TryFrom>` for `std::rc::Rc<[i32; 33]>` @@ -62,11 +62,11 @@ LL | let boxed_array = >::try_from(boxed_slice); | ^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `std::convert::From>` is not implemented for `std::sync::Arc<[i32; 33]>` | = help: the following implementations were found: + as std::convert::From>> as std::convert::From> as std::convert::From>> as std::convert::From<&[T]>> - as std::convert::From>> - and 8 others + and 9 others = note: required because of the requirements on the impl of `std::convert::Into>` for `std::sync::Arc<[i32]>` = note: required because of the requirements on the impl of `std::convert::TryFrom>` for `std::sync::Arc<[i32; 33]>`