From 6d40751b37ef453bee5d8a9c47b00ef205d61738 Mon Sep 17 00:00:00 2001 From: Josh Stone Date: Wed, 22 Apr 2020 13:33:42 -0700 Subject: 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(+) (limited to 'src/liballoc') 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 -- cgit 1.4.1-3-g733a5