Skip to content

Commit 9f32ab8

Browse files
Rollup merge of #89664 - timClicks:51430-document-boxed-conversions, r=m-ou-se
Add documentation to boxed conversions Among other changes, documents whether allocations are necessary to complete the type conversion. Part of #51430, supersedes #89199
2 parents 21a5101 + 020ec0a commit 9f32ab8

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

library/alloc/src/boxed.rs

+41
Original file line numberDiff line numberDiff line change
@@ -1277,6 +1277,7 @@ impl<T> From<T> for Box<T> {
12771277
/// from the stack into it.
12781278
///
12791279
/// # Examples
1280+
///
12801281
/// ```rust
12811282
/// let x = 5;
12821283
/// let boxed = Box::new(5);
@@ -1330,6 +1331,12 @@ impl<T: Copy> From<&[T]> for Box<[T]> {
13301331
#[cfg(not(no_global_oom_handling))]
13311332
#[stable(feature = "box_from_cow", since = "1.45.0")]
13321333
impl<T: Copy> From<Cow<'_, [T]>> for Box<[T]> {
1334+
/// Converts a `Cow<'_, [T]>` into a `Box<[T]>`
1335+
///
1336+
/// When `cow` is the `Cow::Borrowed` variant, this
1337+
/// conversion allocates on the heap and copies the
1338+
/// underlying slice. Otherwise, it will try to reuse the owned
1339+
/// `Vec`'s allocation.
13331340
#[inline]
13341341
fn from(cow: Cow<'_, [T]>) -> Box<[T]> {
13351342
match cow {
@@ -1348,6 +1355,7 @@ impl From<&str> for Box<str> {
13481355
/// and performs a copy of `s`.
13491356
///
13501357
/// # Examples
1358+
///
13511359
/// ```rust
13521360
/// let boxed: Box<str> = Box::from("hello");
13531361
/// println!("{}", boxed);
@@ -1361,6 +1369,29 @@ impl From<&str> for Box<str> {
13611369
#[cfg(not(no_global_oom_handling))]
13621370
#[stable(feature = "box_from_cow", since = "1.45.0")]
13631371
impl From<Cow<'_, str>> for Box<str> {
1372+
/// Converts a `Cow<'_, str>` into a `Box<str>`
1373+
///
1374+
/// When `cow` is the `Cow::Borrowed` variant, this
1375+
/// conversion allocates on the heap and copies the
1376+
/// underlying `str`. Otherwise, it will try to reuse the owned
1377+
/// `String`'s allocation.
1378+
///
1379+
/// # Examples
1380+
///
1381+
/// ```rust
1382+
/// use std::borrow::Cow;
1383+
///
1384+
/// let unboxed = Cow::Borrowed("hello");
1385+
/// let boxed: Box<str> = Box::from(unboxed);
1386+
/// println!("{}", boxed);
1387+
/// ```
1388+
///
1389+
/// ```rust
1390+
/// # use std::borrow::Cow;
1391+
/// let unboxed = Cow::Owned("hello".to_string());
1392+
/// let boxed: Box<str> = Box::from(unboxed);
1393+
/// println!("{}", boxed);
1394+
/// ```
13641395
#[inline]
13651396
fn from(cow: Cow<'_, str>) -> Box<str> {
13661397
match cow {
@@ -1403,6 +1434,7 @@ impl<T, const N: usize> From<[T; N]> for Box<[T]> {
14031434
/// This conversion moves the array to newly heap-allocated memory.
14041435
///
14051436
/// # Examples
1437+
///
14061438
/// ```rust
14071439
/// let boxed: Box<[u8]> = Box::from([4, 2]);
14081440
/// println!("{:?}", boxed);
@@ -1416,6 +1448,15 @@ impl<T, const N: usize> From<[T; N]> for Box<[T]> {
14161448
impl<T, const N: usize> TryFrom<Box<[T]>> for Box<[T; N]> {
14171449
type Error = Box<[T]>;
14181450

1451+
/// Attempts to convert a `Box<[T]>` into a `Box<[T; N]>`.
1452+
///
1453+
/// The conversion occurs in-place and does not require a
1454+
/// new memory allocation.
1455+
///
1456+
/// # Errors
1457+
///
1458+
/// Returns the old `Box<[T]>` in the `Err` variant if
1459+
/// `boxed_slice.len()` does not equal `N`.
14191460
fn try_from(boxed_slice: Box<[T]>) -> Result<Self, Self::Error> {
14201461
if boxed_slice.len() == N {
14211462
Ok(unsafe { Box::from_raw(Box::into_raw(boxed_slice) as *mut [T; N]) })

0 commit comments

Comments
 (0)