Skip to content

Commit 1eb8786

Browse files
authored
Rollup merge of #55470 - daniellimws:box-from-docs, r=Centril
box: Add documentation for `From` impls This is a part of #51430. A brief description of the behaviour and examples are added to the documentation. I am not sure what sort of examples to put for the `From` for `Pin` as my [code](https://play.rust-lang.org/?version=nightly&mode=debug&edition=2015&gist=97c908f44e41c9faeffec5b61d72a03e) doesn't even manage to compile using the nightly build. Somehow I feel that I missed out something so do let me know if more information is needed in the documentation or any of the examples require change.
2 parents 94bf2c1 + 5e2bfda commit 1eb8786

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

src/liballoc/boxed.rs

+54
Original file line numberDiff line numberDiff line change
@@ -443,13 +443,28 @@ impl<T: ?Sized + Hasher> Hasher for Box<T> {
443443

444444
#[stable(feature = "from_for_ptrs", since = "1.6.0")]
445445
impl<T> From<T> for Box<T> {
446+
/// Converts a generic type `T` into a `Box<T>`
447+
///
448+
/// The conversion allocates on the heap and moves `t`
449+
/// from the stack into it.
450+
///
451+
/// # Examples
452+
/// ```rust
453+
/// let x = 5;
454+
/// let boxed = Box::new(5);
455+
///
456+
/// assert_eq!(Box::from(x), boxed);
457+
/// ```
446458
fn from(t: T) -> Self {
447459
Box::new(t)
448460
}
449461
}
450462

451463
#[stable(feature = "pin", since = "1.33.0")]
452464
impl<T> From<Box<T>> for Pin<Box<T>> {
465+
/// Converts a `Box<T>` into a `Pin<Box<T>>`
466+
///
467+
/// This conversion does not allocate on the heap and happens in place.
453468
fn from(boxed: Box<T>) -> Self {
454469
// It's not possible to move or replace the insides of a `Pin<Box<T>>`
455470
// when `T: !Unpin`, so it's safe to pin it directly without any
@@ -460,6 +475,19 @@ impl<T> From<Box<T>> for Pin<Box<T>> {
460475

461476
#[stable(feature = "box_from_slice", since = "1.17.0")]
462477
impl<'a, T: Copy> From<&'a [T]> for Box<[T]> {
478+
/// Converts a `&[T]` into a `Box<[T]>`
479+
///
480+
/// This conversion allocates on the heap
481+
/// and performs a copy of `slice`.
482+
///
483+
/// # Examples
484+
/// ```rust
485+
/// // create a &[u8] which will be used to create a Box<[u8]>
486+
/// let slice: &[u8] = &[104, 101, 108, 108, 111];
487+
/// let boxed_slice: Box<[u8]> = Box::from(slice);
488+
///
489+
/// println!("{:?}", boxed_slice);
490+
/// ```
463491
fn from(slice: &'a [T]) -> Box<[T]> {
464492
let mut boxed = unsafe { RawVec::with_capacity(slice.len()).into_box() };
465493
boxed.copy_from_slice(slice);
@@ -469,6 +497,16 @@ impl<'a, T: Copy> From<&'a [T]> for Box<[T]> {
469497

470498
#[stable(feature = "box_from_slice", since = "1.17.0")]
471499
impl<'a> From<&'a str> for Box<str> {
500+
/// Converts a `&str` into a `Box<str>`
501+
///
502+
/// This conversion allocates on the heap
503+
/// and performs a copy of `s`.
504+
///
505+
/// # Examples
506+
/// ```rust
507+
/// let boxed: Box<str> = Box::from("hello");
508+
/// println!("{}", boxed);
509+
/// ```
472510
#[inline]
473511
fn from(s: &'a str) -> Box<str> {
474512
unsafe { from_boxed_utf8_unchecked(Box::from(s.as_bytes())) }
@@ -477,6 +515,22 @@ impl<'a> From<&'a str> for Box<str> {
477515

478516
#[stable(feature = "boxed_str_conv", since = "1.19.0")]
479517
impl From<Box<str>> for Box<[u8]> {
518+
/// Converts a `Box<str>>` into a `Box<[u8]>`
519+
///
520+
/// This conversion does not allocate on the heap and happens in place.
521+
///
522+
/// # Examples
523+
/// ```rust
524+
/// // create a Box<str> which will be used to create a Box<[u8]>
525+
/// let boxed: Box<str> = Box::from("hello");
526+
/// let boxed_str: Box<[u8]> = Box::from(boxed);
527+
///
528+
/// // create a &[u8] which will be used to create a Box<[u8]>
529+
/// let slice: &[u8] = &[104, 101, 108, 108, 111];
530+
/// let boxed_slice = Box::from(slice);
531+
///
532+
/// assert_eq!(boxed_slice, boxed_str);
533+
/// ```
480534
#[inline]
481535
fn from(s: Box<str>) -> Self {
482536
unsafe { Box::from_raw(Box::into_raw(s) as *mut [u8]) }

0 commit comments

Comments
 (0)