-
Notifications
You must be signed in to change notification settings - Fork 13k
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<&[T]> for &[T; N] #44764
Changes from all commits
1322177
6570e4c
a6e70b5
e45e8ab
e41610c
d2cf66b
d9d8772
4c853ad
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 |
---|---|---|
|
@@ -21,6 +21,7 @@ | |
|
||
use borrow::{Borrow, BorrowMut}; | ||
use cmp::Ordering; | ||
use convert::TryFrom; | ||
use fmt; | ||
use hash::{Hash, self}; | ||
use marker::Unsize; | ||
|
@@ -57,6 +58,30 @@ unsafe impl<T, A: Unsize<[T]>> FixedSizeArray<T> for A { | |
} | ||
} | ||
|
||
/// The error type returned when a conversion from a slice to an array fails. | ||
#[unstable(feature = "try_from", issue = "33417")] | ||
#[derive(Debug, Copy, Clone)] | ||
pub struct TryFromSliceError(()); | ||
|
||
impl fmt::Display for TryFromSliceError { | ||
#[inline] | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
fmt::Display::fmt(self.__description(), f) | ||
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. Why not use the description method from the 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. Error is declined in libstd, not libcore 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. Defined* (I'm on mobile) 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. I see, never mind. |
||
} | ||
} | ||
|
||
impl TryFromSliceError { | ||
#[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 slice to array" | ||
} | ||
} | ||
|
||
macro_rules! __impl_slice_eq1 { | ||
($Lhs: ty, $Rhs: ty) => { | ||
__impl_slice_eq1! { $Lhs, $Rhs, Sized } | ||
|
@@ -123,6 +148,34 @@ macro_rules! array_impls { | |
} | ||
} | ||
|
||
#[unstable(feature = "try_from", issue = "33417")] | ||
impl<'a, T> TryFrom<&'a [T]> for &'a [T; $N] { | ||
type Error = TryFromSliceError; | ||
|
||
fn try_from(slice: &[T]) -> Result<&[T; $N], TryFromSliceError> { | ||
if slice.len() == $N { | ||
let ptr = slice.as_ptr() as *const [T; $N]; | ||
unsafe { Ok(&*ptr) } | ||
} else { | ||
Err(TryFromSliceError(())) | ||
} | ||
} | ||
} | ||
|
||
#[unstable(feature = "try_from", issue = "33417")] | ||
impl<'a, T> TryFrom<&'a mut [T]> for &'a mut [T; $N] { | ||
type Error = TryFromSliceError; | ||
|
||
fn try_from(slice: &mut [T]) -> Result<&mut [T; $N], TryFromSliceError> { | ||
if slice.len() == $N { | ||
let ptr = slice.as_mut_ptr() as *mut [T; $N]; | ||
unsafe { Ok(&mut *ptr) } | ||
} else { | ||
Err(TryFromSliceError(())) | ||
} | ||
} | ||
} | ||
|
||
#[stable(feature = "rust1", since = "1.0.0")] | ||
impl<T: Hash> Hash for [T; $N] { | ||
fn hash<H: hash::Hasher>(&self, state: &mut H) { | ||
|
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.
This should probably implement
Error
in libstd.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.
Should the
Display
implementation also be in libstd or libcore?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.
See e45e8ab