diff options
| author | daniellimws <weesoong.lim@gmail.com> | 2018-10-29 19:15:22 +0800 |
|---|---|---|
| committer | daniellimws <weesoong.lim@gmail.com> | 2018-10-29 19:15:22 +0800 |
| commit | b089e79562aff01cee9b6246f9352ffecef4b3b9 (patch) | |
| tree | a8a244f116c703baf7c8121a30f8e68b55ac7cf2 /src/liballoc | |
| parent | fa45602b71c59c6315fdb07e925dec61f5827ad9 (diff) | |
| download | rust-b089e79562aff01cee9b6246f9352ffecef4b3b9.tar.gz rust-b089e79562aff01cee9b6246f9352ffecef4b3b9.zip | |
Add documentation for From impls
Diffstat (limited to 'src/liballoc')
| -rw-r--r-- | src/liballoc/boxed.rs | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/src/liballoc/boxed.rs b/src/liballoc/boxed.rs index f989e701913..da4651ca779 100644 --- a/src/liballoc/boxed.rs +++ b/src/liballoc/boxed.rs @@ -438,6 +438,18 @@ impl<T: ?Sized + Hasher> Hasher for Box<T> { #[stable(feature = "from_for_ptrs", since = "1.6.0")] impl<T> From<T> for Box<T> { + /// Converts a generic type `T` into a `Box<T>` + /// + /// The conversion allocates on the heap and moves `t` + /// from the stack into it. + /// + /// # Examples + /// ```rust + /// let x = 5; + /// let boxed = Box::new(5); + /// + /// assert_eq!(Box::from(x), boxed); + /// ``` fn from(t: T) -> Self { Box::new(t) } @@ -445,6 +457,9 @@ impl<T> From<T> for Box<T> { #[unstable(feature = "pin", issue = "49150")] impl<T> From<Box<T>> for Pin<Box<T>> { + /// Converts a `Box<T>` into a `Pin<Box<T>>` + /// + /// This conversion does not allocate on the heap and happens in place. fn from(boxed: Box<T>) -> Self { // It's not possible to move or replace the insides of a `Pin<Box<T>>` // 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>> { #[stable(feature = "box_from_slice", since = "1.17.0")] impl<'a, T: Copy> From<&'a [T]> for Box<[T]> { + /// Converts a `&[T]` into a `Box<[T]>` + /// + /// This conversion does not allocate on the heap + /// but performs a copy of `slice`. + /// + /// # Examples + /// ```rust + /// // create a &[u8] which will be used to create a Box<[u8]> + /// let slice: &[u8] = &[104, 101, 108, 108, 111]; + /// let boxed_slice = Box::from(slice); + /// + /// println!({:?}, boxed_slice); + /// ``` fn from(slice: &'a [T]) -> Box<[T]> { let mut boxed = unsafe { RawVec::with_capacity(slice.len()).into_box() }; boxed.copy_from_slice(slice); @@ -464,6 +492,15 @@ impl<'a, T: Copy> From<&'a [T]> for Box<[T]> { #[stable(feature = "box_from_slice", since = "1.17.0")] impl<'a> From<&'a str> for Box<str> { + /// Converts a `&str` into a `Box<str>` + /// + /// This conversion does not allocate on the heap and happens in place. + /// + /// # Examples + /// ```rust + /// let boxed: Box<str> = Box::from("hello"); + /// println!("{}", boxed); + /// ``` #[inline] fn from(s: &'a str) -> Box<str> { unsafe { from_boxed_utf8_unchecked(Box::from(s.as_bytes())) } @@ -472,6 +509,22 @@ impl<'a> From<&'a str> for Box<str> { #[stable(feature = "boxed_str_conv", since = "1.19.0")] impl From<Box<str>> for Box<[u8]> { + /// Converts a `Box<str>>` into a `Box<[u8]>` + /// + /// This conversion does not allocate on the heap and happens in place. + /// + /// # Examples + /// ```rust + /// // create a Box<str> which will be used to create a Box<[u8]> + /// let boxed: Box<str> = Box::from("hello"); + /// let boxed_str: Box<[u8]> = Box::from(boxed); + /// + /// // create a &[u8] which will be used to create a Box<[u8]> + /// let slice: &[u8] = &[104, 101, 108, 108, 111]; + /// let boxed_slice = Box::from(slice); + /// + /// assert_eq!(boxed_slice, boxed_str); + /// ``` #[inline] fn from(s: Box<str>) -> Self { unsafe { Box::from_raw(Box::into_raw(s) as *mut [u8]) } |
