Skip to content

Commit 2673026

Browse files
authored
Rollup merge of #82041 - notriddle:shared-from-slice-docs, r=m-ou-se
Add docs for shared_from_slice From impls The advantage of making these docs is mostly in pointing out that these functions all make new allocations and copy/clone/move the source into them. These docs are on the function, and not the `impl` block, to avoid showing the "[+] show undocumented items" button. CC #51430
2 parents 3560ff3 + 7fafa4d commit 2673026

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed

library/alloc/src/rc.rs

+49
Original file line numberDiff line numberDiff line change
@@ -1652,6 +1652,16 @@ impl<T> From<T> for Rc<T> {
16521652

16531653
#[stable(feature = "shared_from_slice", since = "1.21.0")]
16541654
impl<T: Clone> From<&[T]> for Rc<[T]> {
1655+
/// Allocate a reference-counted slice and fill it by cloning `v`'s items.
1656+
///
1657+
/// # Example
1658+
///
1659+
/// ```
1660+
/// # use std::rc::Rc;
1661+
/// let original: &[i32] = &[1, 2, 3];
1662+
/// let shared: Rc<[i32]> = Rc::from(original);
1663+
/// assert_eq!(&[1, 2, 3], &shared[..]);
1664+
/// ```
16551665
#[inline]
16561666
fn from(v: &[T]) -> Rc<[T]> {
16571667
<Self as RcFromSlice<T>>::from_slice(v)
@@ -1660,6 +1670,15 @@ impl<T: Clone> From<&[T]> for Rc<[T]> {
16601670

16611671
#[stable(feature = "shared_from_slice", since = "1.21.0")]
16621672
impl From<&str> for Rc<str> {
1673+
/// Allocate a reference-counted string slice and copy `v` into it.
1674+
///
1675+
/// # Example
1676+
///
1677+
/// ```
1678+
/// # use std::rc::Rc;
1679+
/// let shared: Rc<str> = Rc::from("statue");
1680+
/// assert_eq!("statue", &shared[..]);
1681+
/// ```
16631682
#[inline]
16641683
fn from(v: &str) -> Rc<str> {
16651684
let rc = Rc::<[u8]>::from(v.as_bytes());
@@ -1669,6 +1688,16 @@ impl From<&str> for Rc<str> {
16691688

16701689
#[stable(feature = "shared_from_slice", since = "1.21.0")]
16711690
impl From<String> for Rc<str> {
1691+
/// Allocate a reference-counted string slice and copy `v` into it.
1692+
///
1693+
/// # Example
1694+
///
1695+
/// ```
1696+
/// # use std::rc::Rc;
1697+
/// let original: String = "statue".to_owned();
1698+
/// let shared: Rc<str> = Rc::from(original);
1699+
/// assert_eq!("statue", &shared[..]);
1700+
/// ```
16721701
#[inline]
16731702
fn from(v: String) -> Rc<str> {
16741703
Rc::from(&v[..])
@@ -1677,6 +1706,16 @@ impl From<String> for Rc<str> {
16771706

16781707
#[stable(feature = "shared_from_slice", since = "1.21.0")]
16791708
impl<T: ?Sized> From<Box<T>> for Rc<T> {
1709+
/// Move a boxed object to a new, reference counted, allocation.
1710+
///
1711+
/// # Example
1712+
///
1713+
/// ```
1714+
/// # use std::rc::Rc;
1715+
/// let original: Box<i32> = Box::new(1);
1716+
/// let shared: Rc<i32> = Rc::from(original);
1717+
/// assert_eq!(1, *shared);
1718+
/// ```
16801719
#[inline]
16811720
fn from(v: Box<T>) -> Rc<T> {
16821721
Rc::from_box(v)
@@ -1685,6 +1724,16 @@ impl<T: ?Sized> From<Box<T>> for Rc<T> {
16851724

16861725
#[stable(feature = "shared_from_slice", since = "1.21.0")]
16871726
impl<T> From<Vec<T>> for Rc<[T]> {
1727+
/// Allocate a reference-counted slice and move `v`'s items into it.
1728+
///
1729+
/// # Example
1730+
///
1731+
/// ```
1732+
/// # use std::rc::Rc;
1733+
/// let original: Box<Vec<i32>> = Box::new(vec![1, 2, 3]);
1734+
/// let shared: Rc<Vec<i32>> = Rc::from(original);
1735+
/// assert_eq!(vec![1, 2, 3], *shared);
1736+
/// ```
16881737
#[inline]
16891738
fn from(mut v: Vec<T>) -> Rc<[T]> {
16901739
unsafe {

library/alloc/src/sync.rs

+49
Original file line numberDiff line numberDiff line change
@@ -2285,6 +2285,16 @@ impl<T> From<T> for Arc<T> {
22852285

22862286
#[stable(feature = "shared_from_slice", since = "1.21.0")]
22872287
impl<T: Clone> From<&[T]> for Arc<[T]> {
2288+
/// Allocate a reference-counted slice and fill it by cloning `v`'s items.
2289+
///
2290+
/// # Example
2291+
///
2292+
/// ```
2293+
/// # use std::sync::Arc;
2294+
/// let original: &[i32] = &[1, 2, 3];
2295+
/// let shared: Arc<[i32]> = Arc::from(original);
2296+
/// assert_eq!(&[1, 2, 3], &shared[..]);
2297+
/// ```
22882298
#[inline]
22892299
fn from(v: &[T]) -> Arc<[T]> {
22902300
<Self as ArcFromSlice<T>>::from_slice(v)
@@ -2293,6 +2303,15 @@ impl<T: Clone> From<&[T]> for Arc<[T]> {
22932303

22942304
#[stable(feature = "shared_from_slice", since = "1.21.0")]
22952305
impl From<&str> for Arc<str> {
2306+
/// Allocate a reference-counted `str` and copy `v` into it.
2307+
///
2308+
/// # Example
2309+
///
2310+
/// ```
2311+
/// # use std::sync::Arc;
2312+
/// let shared: Arc<str> = Arc::from("eggplant");
2313+
/// assert_eq!("eggplant", &shared[..]);
2314+
/// ```
22962315
#[inline]
22972316
fn from(v: &str) -> Arc<str> {
22982317
let arc = Arc::<[u8]>::from(v.as_bytes());
@@ -2302,6 +2321,16 @@ impl From<&str> for Arc<str> {
23022321

23032322
#[stable(feature = "shared_from_slice", since = "1.21.0")]
23042323
impl From<String> for Arc<str> {
2324+
/// Allocate a reference-counted `str` and copy `v` into it.
2325+
///
2326+
/// # Example
2327+
///
2328+
/// ```
2329+
/// # use std::sync::Arc;
2330+
/// let unique: String = "eggplant".to_owned();
2331+
/// let shared: Arc<str> = Arc::from(unique);
2332+
/// assert_eq!("eggplant", &shared[..]);
2333+
/// ```
23052334
#[inline]
23062335
fn from(v: String) -> Arc<str> {
23072336
Arc::from(&v[..])
@@ -2310,6 +2339,16 @@ impl From<String> for Arc<str> {
23102339

23112340
#[stable(feature = "shared_from_slice", since = "1.21.0")]
23122341
impl<T: ?Sized> From<Box<T>> for Arc<T> {
2342+
/// Move a boxed object to a new, reference-counted allocation.
2343+
///
2344+
/// # Example
2345+
///
2346+
/// ```
2347+
/// # use std::sync::Arc;
2348+
/// let unique: Box<str> = Box::from("eggplant");
2349+
/// let shared: Arc<str> = Arc::from(unique);
2350+
/// assert_eq!("eggplant", &shared[..]);
2351+
/// ```
23132352
#[inline]
23142353
fn from(v: Box<T>) -> Arc<T> {
23152354
Arc::from_box(v)
@@ -2318,6 +2357,16 @@ impl<T: ?Sized> From<Box<T>> for Arc<T> {
23182357

23192358
#[stable(feature = "shared_from_slice", since = "1.21.0")]
23202359
impl<T> From<Vec<T>> for Arc<[T]> {
2360+
/// Allocate a reference-counted slice and move `v`'s items into it.
2361+
///
2362+
/// # Example
2363+
///
2364+
/// ```
2365+
/// # use std::sync::Arc;
2366+
/// let unique: Vec<i32> = vec![1, 2, 3];
2367+
/// let shared: Arc<[i32]> = Arc::from(unique);
2368+
/// assert_eq!(&[1, 2, 3], &shared[..]);
2369+
/// ```
23212370
#[inline]
23222371
fn from(mut v: Vec<T>) -> Arc<[T]> {
23232372
unsafe {

0 commit comments

Comments
 (0)