diff --git a/library/alloc/src/rc.rs b/library/alloc/src/rc.rs index f67f5fc533b..dac4acc4692 100644 --- a/library/alloc/src/rc.rs +++ b/library/alloc/src/rc.rs @@ -1652,6 +1652,16 @@ fn from(t: T) -> Self { #[stable(feature = "shared_from_slice", since = "1.21.0")] impl 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]> { >::from_slice(v) @@ -1660,6 +1670,15 @@ fn from(v: &[T]) -> Rc<[T]> { #[stable(feature = "shared_from_slice", since = "1.21.0")] impl From<&str> for Rc { + /// Allocate a reference-counted string slice and copy `v` into it. + /// + /// # Example + /// + /// ``` + /// # use std::rc::Rc; + /// let shared: Rc = Rc::from("statue"); + /// assert_eq!("statue", &shared[..]); + /// ``` #[inline] fn from(v: &str) -> Rc { let rc = Rc::<[u8]>::from(v.as_bytes()); @@ -1669,6 +1688,16 @@ fn from(v: &str) -> Rc { #[stable(feature = "shared_from_slice", since = "1.21.0")] impl From for Rc { + /// 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 = Rc::from(original); + /// assert_eq!("statue", &shared[..]); + /// ``` #[inline] fn from(v: String) -> Rc { Rc::from(&v[..]) @@ -1677,6 +1706,16 @@ fn from(v: String) -> Rc { #[stable(feature = "shared_from_slice", since = "1.21.0")] impl From> for Rc { + /// Move a boxed object to a new, reference counted, allocation. + /// + /// # Example + /// + /// ``` + /// # use std::rc::Rc; + /// let original: Box = Box::new(1); + /// let shared: Rc = Rc::from(original); + /// assert_eq!(1, *shared); + /// ``` #[inline] fn from(v: Box) -> Rc { Rc::from_box(v) @@ -1685,6 +1724,16 @@ fn from(v: Box) -> Rc { #[stable(feature = "shared_from_slice", since = "1.21.0")] impl From> for Rc<[T]> { + /// Allocate a reference-counted slice and move `v`'s items into it. + /// + /// # Example + /// + /// ``` + /// # use std::rc::Rc; + /// let original: Box> = Box::new(vec![1, 2, 3]); + /// let shared: Rc> = Rc::from(original); + /// assert_eq!(vec![1, 2, 3], *shared); + /// ``` #[inline] fn from(mut v: Vec) -> Rc<[T]> { unsafe { diff --git a/library/alloc/src/sync.rs b/library/alloc/src/sync.rs index 461ca85c030..aeae888dddc 100644 --- a/library/alloc/src/sync.rs +++ b/library/alloc/src/sync.rs @@ -2285,6 +2285,16 @@ fn from(t: T) -> Self { #[stable(feature = "shared_from_slice", since = "1.21.0")] impl From<&[T]> for Arc<[T]> { + /// Allocate a reference-counted slice and fill it by cloning `v`'s items. + /// + /// # Example + /// + /// ``` + /// # use std::sync::Arc; + /// let original: &[i32] = &[1, 2, 3]; + /// let shared: Arc<[i32]> = Arc::from(original); + /// assert_eq!(&[1, 2, 3], &shared[..]); + /// ``` #[inline] fn from(v: &[T]) -> Arc<[T]> { >::from_slice(v) @@ -2293,6 +2303,15 @@ fn from(v: &[T]) -> Arc<[T]> { #[stable(feature = "shared_from_slice", since = "1.21.0")] impl From<&str> for Arc { + /// Allocate a reference-counted `str` and copy `v` into it. + /// + /// # Example + /// + /// ``` + /// # use std::sync::Arc; + /// let shared: Arc = Arc::from("eggplant"); + /// assert_eq!("eggplant", &shared[..]); + /// ``` #[inline] fn from(v: &str) -> Arc { let arc = Arc::<[u8]>::from(v.as_bytes()); @@ -2302,6 +2321,16 @@ fn from(v: &str) -> Arc { #[stable(feature = "shared_from_slice", since = "1.21.0")] impl From for Arc { + /// Allocate a reference-counted `str` and copy `v` into it. + /// + /// # Example + /// + /// ``` + /// # use std::sync::Arc; + /// let unique: String = "eggplant".to_owned(); + /// let shared: Arc = Arc::from(unique); + /// assert_eq!("eggplant", &shared[..]); + /// ``` #[inline] fn from(v: String) -> Arc { Arc::from(&v[..]) @@ -2310,6 +2339,16 @@ fn from(v: String) -> Arc { #[stable(feature = "shared_from_slice", since = "1.21.0")] impl From> for Arc { + /// Move a boxed object to a new, reference-counted allocation. + /// + /// # Example + /// + /// ``` + /// # use std::sync::Arc; + /// let unique: Box = Box::from("eggplant"); + /// let shared: Arc = Arc::from(unique); + /// assert_eq!("eggplant", &shared[..]); + /// ``` #[inline] fn from(v: Box) -> Arc { Arc::from_box(v) @@ -2318,6 +2357,16 @@ fn from(v: Box) -> Arc { #[stable(feature = "shared_from_slice", since = "1.21.0")] impl From> for Arc<[T]> { + /// Allocate a reference-counted slice and move `v`'s items into it. + /// + /// # Example + /// + /// ``` + /// # use std::sync::Arc; + /// let unique: Vec = vec![1, 2, 3]; + /// let shared: Arc<[i32]> = Arc::from(unique); + /// assert_eq!(&[1, 2, 3], &shared[..]); + /// ``` #[inline] fn from(mut v: Vec) -> Arc<[T]> { unsafe {