Implement TryFrom<&'_ mut [T]>
for [T; N]
This commit is contained in:
parent
6d48ee90f5
commit
72b411fd89
@ -176,6 +176,18 @@ fn borrow_mut(&mut self) -> &mut [T] {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[stable(feature = "try_from_mut_slice_to_array", since = "1.57.0")]
|
||||||
|
impl<T, const N: usize> TryFrom<&mut [T]> for [T; N]
|
||||||
|
where
|
||||||
|
T: Copy,
|
||||||
|
{
|
||||||
|
type Error = TryFromSliceError;
|
||||||
|
|
||||||
|
fn try_from(slice: &mut [T]) -> Result<[T; N], TryFromSliceError> {
|
||||||
|
<Self>::try_from(slice.as_ref())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[stable(feature = "try_from", since = "1.34.0")]
|
#[stable(feature = "try_from", since = "1.34.0")]
|
||||||
impl<'a, T, const N: usize> TryFrom<&'a [T]> for &'a [T; N] {
|
impl<'a, T, const N: usize> TryFrom<&'a [T]> for &'a [T; N] {
|
||||||
type Error = TryFromSliceError;
|
type Error = TryFromSliceError;
|
||||||
|
@ -28,11 +28,22 @@ macro_rules! test {
|
|||||||
($($N:expr)+) => {
|
($($N:expr)+) => {
|
||||||
$({
|
$({
|
||||||
type Array = [u8; $N];
|
type Array = [u8; $N];
|
||||||
let array: Array = [0; $N];
|
let mut array: Array = [0; $N];
|
||||||
let slice: &[u8] = &array[..];
|
let slice: &[u8] = &array[..];
|
||||||
|
|
||||||
let result = <&Array>::try_from(slice);
|
let result = <&Array>::try_from(slice);
|
||||||
assert_eq!(&array, result.unwrap());
|
assert_eq!(&array, result.unwrap());
|
||||||
|
|
||||||
|
let result = <Array>::try_from(slice);
|
||||||
|
assert_eq!(&array, &result.unwrap());
|
||||||
|
|
||||||
|
let mut_slice: &mut [u8] = &mut array[..];
|
||||||
|
let result = <&mut Array>::try_from(mut_slice);
|
||||||
|
assert_eq!(&[0; $N], result.unwrap());
|
||||||
|
|
||||||
|
let mut_slice: &mut [u8] = &mut array[..];
|
||||||
|
let result = <Array>::try_from(mut_slice);
|
||||||
|
assert_eq!(&array, &result.unwrap());
|
||||||
})+
|
})+
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user