From 5978b0d26db1261af762dc276627519617133ceb Mon Sep 17 00:00:00 2001 From: yameste Date: Sat, 23 Jun 2018 17:55:11 +0100 Subject: [PATCH] Many `From` implementations are undocumented #51430 --- src/liballoc/boxed.rs | 68 ++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 64 insertions(+), 4 deletions(-) diff --git a/src/liballoc/boxed.rs b/src/liballoc/boxed.rs index ea60c7775af59..5dcd16c90a007 100644 --- a/src/liballoc/boxed.rs +++ b/src/liballoc/boxed.rs @@ -414,38 +414,74 @@ impl Hasher for Box { } } +/// First From #[stable(feature = "from_for_ptrs", since = "1.6.0")] impl From for Box { + /// Converts a T into a Box. + /// This conversion does allocate memory. + /// This conversion is not free. + fn from(t: T) -> Self { Box::new(t) } + /// The result is allocated on the heap. + /// The conversion copies the data. + /// This conversion is more expensive because the is not already on the heap. + } - +/// Second From #[stable(feature = "box_from_slice", since = "1.17.0")] impl<'a, T: Copy> From<&'a [T]> for Box<[T]> { + /// Copies a T in to a box. + /// This conversion copies the data. + /// This conversion does allocate memory. + /// fn from(slice: &'a [T]) -> Box<[T]> { let mut boxed = unsafe { RawVec::with_capacity(slice.len()).into_box() }; boxed.copy_from_slice(slice); boxed } + /// The result is allocated on the heap + /// This is cheaper because it is already on the heap + /// and the pointer moves along + } +/// Third From #[stable(feature = "box_from_slice", since = "1.17.0")] impl<'a> From<&'a str> for Box { + /// Copies a string in to a box. + /// This conversion copies the data. + /// This conversion does allocate memory. #[inline] fn from(s: &'a str) -> Box { unsafe { from_boxed_utf8_unchecked(Box::from(s.as_bytes())) } } -} + /// The result is not allocated on the heap + /// This function is unsafe. Data can't be moved from this reference. + /// The conversion is slightly more expensive because the string is not + /// on the heap. + +} +/// Fourth From #[stable(feature = "boxed_str_conv", since = "1.19.0")] impl From> for Box<[u8]> { + /// Converts a string within a Box in to an + /// integer within the Box. + /// This conversion happens in place. + /// This conversion does not allocate memory. + #[inline] fn from(s: Box) -> Self { unsafe { Box::from_raw(Box::into_raw(s) as *mut [u8]) } } + /// This conversion is cheaper because the string is already + /// on the heap. + /// This function is unsafe. Data can't be moved from this reference. } + impl Box { #[inline] #[stable(feature = "rust1", since = "1.0.0")] @@ -854,20 +890,32 @@ impl PinBox { } } +/// Fifth From #[unstable(feature = "pin", issue = "49150")] impl From> for PinBox { + /// Converts an Sized Box in to a PinBox. + /// This conversion copies the data. + /// This conversion does allocate memory. fn from(boxed: Box) -> PinBox { PinBox { inner: boxed } } + /// This conversion is cheaper because the unpinned box is already on the + /// heap. } - +/// Sixth From #[unstable(feature = "pin", issue = "49150")] impl From> for Box { + /// Converts a PinBox in to Box. + /// This conversion copies the data. + /// This conversion does allocate the memory. fn from(pinned: PinBox) -> Box { pinned.inner } + /// This conversion is cheaper because the box is already on the + /// heap. } + #[unstable(feature = "pin", issue = "49150")] impl Deref for PinBox { type Target = T; @@ -949,16 +997,28 @@ unsafe impl + Send + 'static> UnsafeTask for PinBox { } } +/// Seventh From #[unstable(feature = "futures_api", issue = "50547")] impl + Send + 'static> From> for TaskObj { + /// Converts a Pinbox in to a Task Object. + /// This conversion copies the data. + /// This conversion does allocate. + /// fn from(boxed: PinBox) -> Self { TaskObj::new(boxed) } -} + /// This conversion is cheaper because the PinBox is already on the heap. +} +/// Eighth From #[unstable(feature = "futures_api", issue = "50547")] impl + Send + 'static> From> for TaskObj { + /// Converts a Box in to a Task Object. + /// This conversion copies the data. + /// This conversion does allocate. fn from(boxed: Box) -> Self { TaskObj::new(PinBox::from(boxed)) } + /// This conversion is cheaper because the Box is already on the heap. + }