std
implementation:
#[cfg(not(no_global_oom_handling))]
#[stable(feature = "shared_from_slice", since = "1.21.0")]
impl<T: Clone> From<&[T]> for Rc<[T]> {
/// Allocate a reference-counted slice and fill it by cloning `v`'s items.
///
/// # Example
///
/// ```
/// # use std::rc::Rc;
/// let original: &[i32] = &[1, 2, 3];
/// let shared: Rc<[i32]> = Rc::from(original);
/// assert_eq!(&[1, 2, 3], &shared[..]);
/// ```
#[inline]
fn from(v: &[T]) -> Rc<[T]> {
<Self as RcFromSlice<T>>::from_slice(v)
}
}
#[cfg(not(no_global_oom_handling))]
#[stable(feature = "shared_from_slice", since = "1.21.0")]
impl From<&str> for Rc<str> {
/// Allocate a reference-counted string slice and copy `v` into it.
///
/// # Example
///
/// ```
/// # use std::rc::Rc;
/// let shared: Rc<str> = Rc::from("statue");
/// assert_eq!("statue", &shared[..]);
/// ```
#[inline]
fn from(v: &str) -> Rc<str> {
let rc = Rc::<[u8]>::from(v.as_bytes());
unsafe { Rc::from_raw(Rc::into_raw(rc) as *const str) }
}
}
#[cfg(not(no_global_oom_handling))]
#[stable(feature = "shared_from_slice", since = "1.21.0")]
impl From<String> for Rc<str> {
/// Allocate a reference-counted string slice and copy `v` into it.
///
/// # Example
///
/// ```
/// # use std::rc::Rc;
/// let original: String = "statue".to_owned();
/// let shared: Rc<str> = Rc::from(original);
/// assert_eq!("statue", &shared[..]);
/// ```
#[inline]
fn from(v: String) -> Rc<str> {
Rc::from(&v[..])
}
}
#[cfg(not(no_global_oom_handling))]
#[stable(feature = "shared_from_slice", since = "1.21.0")]
impl<T: ?Sized, A: Allocator> From<Box<T, A>> for Rc<T, A> {
/// Move a boxed object to a new, reference counted, allocation.
///
/// # Example
///
/// ```
/// # use std::rc::Rc;
/// let original: Box<i32> = Box::new(1);
/// let shared: Rc<i32> = Rc::from(original);
/// assert_eq!(1, *shared);
/// ```
#[inline]
fn from(v: Box<T, A>) -> Rc<T, A> {
Rc::from_box_in(v)
}
}
The String
is first borrowed as &str
, then converted to &[u8]
, and copied byte by byte to construct the new buffer managed by the returned Rc<str>
. The original String
is then dropped.
Why doesn't impl From<String> for Rc<str>
just move the String
and reuse its underlying buffer?
And that means it's more sensible to use:
fn consume_string(owned_string: String) {
let rc_str: Rc<str> = owned_string.into_boxed_str().into();
instead of:
fn consume_string(owned_string: String) {
let rc_str: Rc<str> = owned_string.into();
Because the latter, although works as documented, doesn't work as expected?
PS: impl From<String> for Arc<str>
shares the same issue.