impl From<Cow> for Rc and Arc

These forward `Borrowed`/`Owned` values to existing `From` impls.

    impl<'a, B> From<Cow<'a, B>> for Rc<B>
    where
        B: ToOwned + ?Sized,
        Rc<B>: From<&'a B> + From<B::Owned>,

    impl<'a, B> From<Cow<'a, B>> for Arc<B>
    where
        B: ToOwned + ?Sized,
        Arc<B>: From<&'a B> + From<B::Owned>,
This commit is contained in:
Josh Stone 2020-04-22 13:33:42 -07:00
parent b0fb57bd8d
commit 6d40751b37
2 changed files with 32 additions and 0 deletions

View file

@ -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<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

View file

@ -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<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