diff options
| author | Mazdak Farrokhzad <twingoow@gmail.com> | 2018-12-24 13:29:26 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2018-12-24 13:29:26 +0100 |
| commit | 1eb87869d17e8906f2fdbab0ea6323b85742b532 (patch) | |
| tree | e5616273a03fcdbf13c8f89d9252749c8b633eb3 /src/liballoc | |
| parent | 94bf2c15eb3d072e92855a605783a29920df9e46 (diff) | |
| parent | 5e2bfda19af0f00707ff0c19547127f902d36b97 (diff) | |
| download | rust-1eb87869d17e8906f2fdbab0ea6323b85742b532.tar.gz rust-1eb87869d17e8906f2fdbab0ea6323b85742b532.zip | |
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.
Diffstat (limited to 'src/liballoc')
| -rw-r--r-- | src/liballoc/boxed.rs | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/src/liballoc/boxed.rs b/src/liballoc/boxed.rs index 7438f3e6c9d..859749780b4 100644 --- a/src/liballoc/boxed.rs +++ b/src/liballoc/boxed.rs @@ -443,6 +443,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) } @@ -450,6 +462,9 @@ impl<T> From<T> for Box<T> { #[stable(feature = "pin", since = "1.33.0")] 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 @@ -460,6 +475,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 allocates on the heap + /// and 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<[u8]> = 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); @@ -469,6 +497,16 @@ 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 allocates on the heap + /// and performs a copy of `s`. + /// + /// # 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())) } @@ -477,6 +515,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]) } |
