Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

impl From<Cow> for Box, Rc, and Arc #71447

Merged
merged 4 commits into from
May 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/liballoc/boxed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -774,6 +775,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>`
Expand All @@ -792,6 +804,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]>`
Expand Down
16 changes: 16 additions & 0 deletions src/liballoc/rc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -1492,6 +1493,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
Expand Down
16 changes: 16 additions & 0 deletions src/liballoc/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -2047,6 +2048,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
Expand Down
47 changes: 47 additions & 0 deletions src/liballoc/tests/borrow.rs
Original file line number Diff line number Diff line change
@@ -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);
}
1 change: 1 addition & 0 deletions src/liballoc/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use std::hash::{Hash, Hasher};

mod arc;
mod binary_heap;
mod borrow;
mod boxed;
mod btree;
mod cow_str;
Expand Down
11 changes: 11 additions & 0 deletions src/libstd/ffi/c_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -730,6 +730,17 @@ impl From<&CStr> for Box<CStr> {
}
}

#[stable(feature = "box_from_cow", since = "1.45.0")]
impl From<Cow<'_, CStr>> for Box<CStr> {
#[inline]
fn from(cow: Cow<'_, CStr>) -> Box<CStr> {
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<Box<CStr>> for CString {
/// Converts a [`Box`]`<CStr>` into a [`CString`] without copying or allocating.
Expand Down
11 changes: 11 additions & 0 deletions src/libstd/ffi/os_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -849,6 +849,17 @@ impl From<&OsStr> for Box<OsStr> {
}
}

#[stable(feature = "box_from_cow", since = "1.45.0")]
impl From<Cow<'_, OsStr>> for Box<OsStr> {
#[inline]
fn from(cow: Cow<'_, OsStr>) -> Box<OsStr> {
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<Box<OsStr>> for OsString {
/// Converts a [`Box`]`<`[`OsStr`]`>` into a `OsString` without copying or
Expand Down
11 changes: 11 additions & 0 deletions src/libstd/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1433,6 +1433,17 @@ impl From<&Path> for Box<Path> {
}
}

#[stable(feature = "box_from_cow", since = "1.45.0")]
impl From<Cow<'_, Path>> for Box<Path> {
#[inline]
fn from(cow: Cow<'_, Path>) -> Box<Path> {
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<Box<Path>> for PathBuf {
/// Converts a `Box<Path>` into a `PathBuf`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ LL | let boxed_array = <Box<[i32; 33]>>::try_from(boxed_slice);
<std::boxed::Box<(dyn std::error::Error + 'static)> as std::convert::From<&str>>
<std::boxed::Box<(dyn std::error::Error + 'static)> as std::convert::From<std::borrow::Cow<'a, str>>>
<std::boxed::Box<(dyn std::error::Error + 'static)> as std::convert::From<std::string::String>>
and 16 others
and 21 others
= note: required because of the requirements on the impl of `std::convert::Into<std::boxed::Box<[i32; 33]>>` for `std::boxed::Box<[i32]>`
= note: required because of the requirements on the impl of `std::convert::TryFrom<std::boxed::Box<[i32]>>` for `std::boxed::Box<[i32; 33]>`

Expand All @@ -38,11 +38,11 @@ LL | let boxed_array = <Rc<[i32; 33]>>::try_from(boxed_slice);
| ^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `std::convert::From<std::rc::Rc<[i32]>>` is not implemented for `std::rc::Rc<[i32; 33]>`
|
= help: the following implementations were found:
<std::rc::Rc<B> as std::convert::From<std::borrow::Cow<'a, B>>>
<std::rc::Rc<T> as std::convert::From<T>>
<std::rc::Rc<T> as std::convert::From<std::boxed::Box<T>>>
<std::rc::Rc<[T]> as std::convert::From<&[T]>>
<std::rc::Rc<[T]> as std::convert::From<std::vec::Vec<T>>>
and 8 others
and 9 others
= note: required because of the requirements on the impl of `std::convert::Into<std::rc::Rc<[i32; 33]>>` for `std::rc::Rc<[i32]>`
= note: required because of the requirements on the impl of `std::convert::TryFrom<std::rc::Rc<[i32]>>` for `std::rc::Rc<[i32; 33]>`

Expand All @@ -62,11 +62,11 @@ LL | let boxed_array = <Arc<[i32; 33]>>::try_from(boxed_slice);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `std::convert::From<std::sync::Arc<[i32]>>` is not implemented for `std::sync::Arc<[i32; 33]>`
|
= help: the following implementations were found:
<std::sync::Arc<B> as std::convert::From<std::borrow::Cow<'a, B>>>
<std::sync::Arc<T> as std::convert::From<T>>
<std::sync::Arc<T> as std::convert::From<std::boxed::Box<T>>>
<std::sync::Arc<[T]> as std::convert::From<&[T]>>
<std::sync::Arc<[T]> as std::convert::From<std::vec::Vec<T>>>
and 8 others
and 9 others
= note: required because of the requirements on the impl of `std::convert::Into<std::sync::Arc<[i32; 33]>>` for `std::sync::Arc<[i32]>`
= note: required because of the requirements on the impl of `std::convert::TryFrom<std::sync::Arc<[i32]>>` for `std::sync::Arc<[i32; 33]>`

Expand Down