@@ -1277,6 +1277,7 @@ impl<T> From<T> for Box<T> {
1277
1277
/// from the stack into it.
1278
1278
///
1279
1279
/// # Examples
1280
+ ///
1280
1281
/// ```rust
1281
1282
/// let x = 5;
1282
1283
/// let boxed = Box::new(5);
@@ -1330,6 +1331,12 @@ impl<T: Copy> From<&[T]> for Box<[T]> {
1330
1331
#[ cfg( not( no_global_oom_handling) ) ]
1331
1332
#[ stable( feature = "box_from_cow" , since = "1.45.0" ) ]
1332
1333
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.
1333
1340
#[ inline]
1334
1341
fn from ( cow : Cow < ' _ , [ T ] > ) -> Box < [ T ] > {
1335
1342
match cow {
@@ -1348,6 +1355,7 @@ impl From<&str> for Box<str> {
1348
1355
/// and performs a copy of `s`.
1349
1356
///
1350
1357
/// # Examples
1358
+ ///
1351
1359
/// ```rust
1352
1360
/// let boxed: Box<str> = Box::from("hello");
1353
1361
/// println!("{}", boxed);
@@ -1361,6 +1369,29 @@ impl From<&str> for Box<str> {
1361
1369
#[ cfg( not( no_global_oom_handling) ) ]
1362
1370
#[ stable( feature = "box_from_cow" , since = "1.45.0" ) ]
1363
1371
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
+ /// ```
1364
1395
#[ inline]
1365
1396
fn from ( cow : Cow < ' _ , str > ) -> Box < str > {
1366
1397
match cow {
@@ -1403,6 +1434,7 @@ impl<T, const N: usize> From<[T; N]> for Box<[T]> {
1403
1434
/// This conversion moves the array to newly heap-allocated memory.
1404
1435
///
1405
1436
/// # Examples
1437
+ ///
1406
1438
/// ```rust
1407
1439
/// let boxed: Box<[u8]> = Box::from([4, 2]);
1408
1440
/// println!("{:?}", boxed);
@@ -1416,6 +1448,15 @@ impl<T, const N: usize> From<[T; N]> for Box<[T]> {
1416
1448
impl < T , const N : usize > TryFrom < Box < [ T ] > > for Box < [ T ; N ] > {
1417
1449
type Error = Box < [ T ] > ;
1418
1450
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`.
1419
1460
fn try_from ( boxed_slice : Box < [ T ] > ) -> Result < Self , Self :: Error > {
1420
1461
if boxed_slice. len ( ) == N {
1421
1462
Ok ( unsafe { Box :: from_raw ( Box :: into_raw ( boxed_slice) as * mut [ T ; N ] ) } )
0 commit comments