@@ -438,13 +438,28 @@ impl<T: ?Sized + Hasher> Hasher for Box<T> {
438
438
439
439
#[ stable( feature = "from_for_ptrs" , since = "1.6.0" ) ]
440
440
impl < T > From < T > for Box < T > {
441
+ /// Converts a generic type `T` into a `Box<T>`
442
+ ///
443
+ /// The conversion allocates on the heap and moves `t`
444
+ /// from the stack into it.
445
+ ///
446
+ /// # Examples
447
+ /// ```rust
448
+ /// let x = 5;
449
+ /// let boxed = Box::new(5);
450
+ ///
451
+ /// assert_eq!(Box::from(x), boxed);
452
+ /// ```
441
453
fn from ( t : T ) -> Self {
442
454
Box :: new ( t)
443
455
}
444
456
}
445
457
446
458
#[ unstable( feature = "pin" , issue = "49150" ) ]
447
459
impl < T > From < Box < T > > for Pin < Box < T > > {
460
+ /// Converts a `Box<T>` into a `Pin<Box<T>>`
461
+ ///
462
+ /// This conversion does not allocate on the heap and happens in place.
448
463
fn from ( boxed : Box < T > ) -> Self {
449
464
// It's not possible to move or replace the insides of a `Pin<Box<T>>`
450
465
// when `T: !Unpin`, so it's safe to pin it directly without any
@@ -455,6 +470,19 @@ impl<T> From<Box<T>> for Pin<Box<T>> {
455
470
456
471
#[ stable( feature = "box_from_slice" , since = "1.17.0" ) ]
457
472
impl < ' a , T : Copy > From < & ' a [ T ] > for Box < [ T ] > {
473
+ /// Converts a `&[T]` into a `Box<[T]>`
474
+ ///
475
+ /// This conversion does not allocate on the heap
476
+ /// but performs a copy of `slice`.
477
+ ///
478
+ /// # Examples
479
+ /// ```rust
480
+ /// // create a &[u8] which will be used to create a Box<[u8]>
481
+ /// let slice: &[u8] = &[104, 101, 108, 108, 111];
482
+ /// let boxed_slice = Box::from(slice);
483
+ ///
484
+ /// println!({:?}, boxed_slice);
485
+ /// ```
458
486
fn from ( slice : & ' a [ T ] ) -> Box < [ T ] > {
459
487
let mut boxed = unsafe { RawVec :: with_capacity ( slice. len ( ) ) . into_box ( ) } ;
460
488
boxed. copy_from_slice ( slice) ;
@@ -464,6 +492,15 @@ impl<'a, T: Copy> From<&'a [T]> for Box<[T]> {
464
492
465
493
#[ stable( feature = "box_from_slice" , since = "1.17.0" ) ]
466
494
impl < ' a > From < & ' a str > for Box < str > {
495
+ /// Converts a `&str` into a `Box<str>`
496
+ ///
497
+ /// This conversion does not allocate on the heap and happens in place.
498
+ ///
499
+ /// # Examples
500
+ /// ```rust
501
+ /// let boxed: Box<str> = Box::from("hello");
502
+ /// println!("{}", boxed);
503
+ /// ```
467
504
#[ inline]
468
505
fn from ( s : & ' a str ) -> Box < str > {
469
506
unsafe { from_boxed_utf8_unchecked ( Box :: from ( s. as_bytes ( ) ) ) }
@@ -472,6 +509,22 @@ impl<'a> From<&'a str> for Box<str> {
472
509
473
510
#[ stable( feature = "boxed_str_conv" , since = "1.19.0" ) ]
474
511
impl From < Box < str > > for Box < [ u8 ] > {
512
+ /// Converts a `Box<str>>` into a `Box<[u8]>`
513
+ ///
514
+ /// This conversion does not allocate on the heap and happens in place.
515
+ ///
516
+ /// # Examples
517
+ /// ```rust
518
+ /// // create a Box<str> which will be used to create a Box<[u8]>
519
+ /// let boxed: Box<str> = Box::from("hello");
520
+ /// let boxed_str: Box<[u8]> = Box::from(boxed);
521
+ ///
522
+ /// // create a &[u8] which will be used to create a Box<[u8]>
523
+ /// let slice: &[u8] = &[104, 101, 108, 108, 111];
524
+ /// let boxed_slice = Box::from(slice);
525
+ ///
526
+ /// assert_eq!(boxed_slice, boxed_str);
527
+ /// ```
475
528
#[ inline]
476
529
fn from ( s : Box < str > ) -> Self {
477
530
unsafe { Box :: from_raw ( Box :: into_raw ( s) as * mut [ u8 ] ) }
0 commit comments