Skip to content

Commit b089e79

Browse files
committed
Add documentation for From impls
1 parent fa45602 commit b089e79

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

src/liballoc/boxed.rs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,13 +438,28 @@ impl<T: ?Sized + Hasher> Hasher for Box<T> {
438438

439439
#[stable(feature = "from_for_ptrs", since = "1.6.0")]
440440
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+
/// ```
441453
fn from(t: T) -> Self {
442454
Box::new(t)
443455
}
444456
}
445457

446458
#[unstable(feature = "pin", issue = "49150")]
447459
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.
448463
fn from(boxed: Box<T>) -> Self {
449464
// It's not possible to move or replace the insides of a `Pin<Box<T>>`
450465
// 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>> {
455470

456471
#[stable(feature = "box_from_slice", since = "1.17.0")]
457472
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+
/// ```
458486
fn from(slice: &'a [T]) -> Box<[T]> {
459487
let mut boxed = unsafe { RawVec::with_capacity(slice.len()).into_box() };
460488
boxed.copy_from_slice(slice);
@@ -464,6 +492,15 @@ impl<'a, T: Copy> From<&'a [T]> for Box<[T]> {
464492

465493
#[stable(feature = "box_from_slice", since = "1.17.0")]
466494
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+
/// ```
467504
#[inline]
468505
fn from(s: &'a str) -> Box<str> {
469506
unsafe { from_boxed_utf8_unchecked(Box::from(s.as_bytes())) }
@@ -472,6 +509,22 @@ impl<'a> From<&'a str> for Box<str> {
472509

473510
#[stable(feature = "boxed_str_conv", since = "1.19.0")]
474511
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+
/// ```
475528
#[inline]
476529
fn from(s: Box<str>) -> Self {
477530
unsafe { Box::from_raw(Box::into_raw(s) as *mut [u8]) }

0 commit comments

Comments
 (0)