-
Notifications
You must be signed in to change notification settings - Fork 13.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement TryFrom<Box<[T]>> for Box<[T; N]> #47245
Changes from all commits
22c581b
b25334a
8cb0b3e
45d6839
da9a570
909e35f
aba11fd
544fb2a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -61,6 +61,7 @@ use raw_vec::RawVec; | |
use core::any::Any; | ||
use core::borrow; | ||
use core::cmp::Ordering; | ||
use core::convert::TryFrom; | ||
use core::fmt; | ||
use core::hash::{self, Hash, Hasher}; | ||
use core::iter::FusedIterator; | ||
|
@@ -623,6 +624,79 @@ impl From<Box<str>> for Box<[u8]> { | |
} | ||
} | ||
|
||
/// The error type returned when a conversion from a boxed slice to a boxed | ||
/// array fails. | ||
#[unstable(feature = "try_from", issue = "33417")] | ||
#[derive(Clone)] | ||
pub struct TryFromSliceError<T>(Box<[T]>); | ||
|
||
#[unstable(feature = "try_from", issue = "33417")] | ||
impl<T> fmt::Debug for TryFromSliceError<T> { | ||
#[inline] | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
f.debug_struct("TryFromSliceError").finish() | ||
} | ||
} | ||
|
||
#[unstable(feature = "try_from", issue = "33417")] | ||
impl<T> fmt::Display for TryFromSliceError<T> { | ||
#[inline] | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
fmt::Display::fmt(self.__description(), f) | ||
} | ||
} | ||
|
||
impl<T> TryFromSliceError<T> { | ||
#[unstable(feature = "array_error_internals", | ||
reason = "available through Error trait and this method should not \ | ||
be exposed publicly", | ||
issue = "0")] | ||
#[inline] | ||
#[doc(hidden)] | ||
pub fn __description(&self) -> &str { | ||
"could not convert boxed slice to boxed array" | ||
} | ||
|
||
/// Returns the boxed slice that was attempted to convert to a boxed array. | ||
/// | ||
/// This method is meant to avoid allocation. It will consume the error, | ||
/// moving out the boxed slice, so that a copy of the slice does not need to | ||
/// be made. | ||
#[unstable(feature = "try_from", issue = "33417")] | ||
#[inline] | ||
pub fn into_boxed_slice(self) -> Box<[T]> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add an example showing a case in which this method is useful. The point of the example wouldn't be to show how to call this method (we can assume readers know how to call methods when they get to browsing TryFrom documentation) but to show why someone would want to call this method. |
||
self.0 | ||
} | ||
} | ||
|
||
macro_rules! array_impls { | ||
($($N:expr)+) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You forgot to add
|
||
$( | ||
#[unstable(feature = "try_from", issue = "33417")] | ||
impl<T> TryFrom<Box<[T]>> for Box<[T; $N]> { | ||
type Error = TryFromSliceError<T>; | ||
|
||
#[inline] | ||
fn try_from(slice: Box<[T]>) -> Result<Box<[T; $N]>, TryFromSliceError<T>> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add a test so that we don't break this implementation later. Something similar to the test in #44764 should work. |
||
if slice.len() == $N { | ||
let ptr = Box::into_raw(slice) as *mut [T; $N]; | ||
unsafe { Ok(Box::from_raw(ptr)) } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @alexcrichton is this a valid operation? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. AFAIK yeah this should be safe, the destructor for |
||
} else { | ||
Err(TryFromSliceError(slice)) | ||
} | ||
} | ||
} | ||
)+ | ||
} | ||
} | ||
|
||
array_impls! { | ||
0 1 2 3 4 5 6 7 8 9 | ||
10 11 12 13 14 15 16 17 18 19 | ||
20 21 22 23 24 25 26 27 28 29 | ||
30 31 32 | ||
} | ||
|
||
impl Box<Any> { | ||
#[inline] | ||
#[stable(feature = "rust1", since = "1.0.0")] | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a brief example involving TryFromSliceError. See FromUtf8Error for examples of examples.