Skip to content

Commit 6d40751

Browse files
committed
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>,
1 parent b0fb57b commit 6d40751

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

src/liballoc/rc.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ use core::ptr::{self, NonNull};
252252
use core::slice::{self, from_raw_parts_mut};
253253

254254
use crate::alloc::{box_free, handle_alloc_error, AllocInit, AllocRef, Global, Layout};
255+
use crate::borrow::{Cow, ToOwned};
255256
use crate::string::String;
256257
use crate::vec::Vec;
257258

@@ -1492,6 +1493,21 @@ impl<T> From<Vec<T>> for Rc<[T]> {
14921493
}
14931494
}
14941495

1496+
#[stable(feature = "shared_from_cow", since = "1.45.0")]
1497+
impl<'a, B> From<Cow<'a, B>> for Rc<B>
1498+
where
1499+
B: ToOwned + ?Sized,
1500+
Rc<B>: From<&'a B> + From<B::Owned>,
1501+
{
1502+
#[inline]
1503+
fn from(cow: Cow<'a, B>) -> Rc<B> {
1504+
match cow {
1505+
Cow::Borrowed(s) => Rc::from(s),
1506+
Cow::Owned(s) => Rc::from(s),
1507+
}
1508+
}
1509+
}
1510+
14951511
#[stable(feature = "boxed_slice_try_from", since = "1.43.0")]
14961512
impl<T, const N: usize> TryFrom<Rc<[T]>> for Rc<[T; N]>
14971513
where

src/liballoc/sync.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ use core::sync::atomic;
2525
use core::sync::atomic::Ordering::{Acquire, Relaxed, Release, SeqCst};
2626

2727
use crate::alloc::{box_free, handle_alloc_error, AllocInit, AllocRef, Global, Layout};
28+
use crate::borrow::{Cow, ToOwned};
2829
use crate::boxed::Box;
2930
use crate::rc::is_dangling;
3031
use crate::string::String;
@@ -2047,6 +2048,21 @@ impl<T> From<Vec<T>> for Arc<[T]> {
20472048
}
20482049
}
20492050

2051+
#[stable(feature = "shared_from_cow", since = "1.45.0")]
2052+
impl<'a, B> From<Cow<'a, B>> for Arc<B>
2053+
where
2054+
B: ToOwned + ?Sized,
2055+
Arc<B>: From<&'a B> + From<B::Owned>,
2056+
{
2057+
#[inline]
2058+
fn from(cow: Cow<'a, B>) -> Arc<B> {
2059+
match cow {
2060+
Cow::Borrowed(s) => Arc::from(s),
2061+
Cow::Owned(s) => Arc::from(s),
2062+
}
2063+
}
2064+
}
2065+
20502066
#[stable(feature = "boxed_slice_try_from", since = "1.43.0")]
20512067
impl<T, const N: usize> TryFrom<Arc<[T]>> for Arc<[T; N]>
20522068
where

0 commit comments

Comments
 (0)