diff --git a/library/core/src/array/mod.rs b/library/core/src/array/mod.rs index 09bb4519170da..33f0f46a0a165 100644 --- a/library/core/src/array/mod.rs +++ b/library/core/src/array/mod.rs @@ -176,6 +176,18 @@ where } } +#[stable(feature = "try_from_mut_slice_to_array", since = "1.59.0")] +impl TryFrom<&mut [T]> for [T; N] +where + T: Copy, +{ + type Error = TryFromSliceError; + + fn try_from(slice: &mut [T]) -> Result<[T; N], TryFromSliceError> { + ::try_from(&*slice) + } +} + #[stable(feature = "try_from", since = "1.34.0")] impl<'a, T, const N: usize> TryFrom<&'a [T]> for &'a [T; N] { type Error = TryFromSliceError; diff --git a/library/core/tests/array.rs b/library/core/tests/array.rs index 7dc071b74235d..1f53cdf200a59 100644 --- a/library/core/tests/array.rs +++ b/library/core/tests/array.rs @@ -28,11 +28,22 @@ fn array_try_from() { ($($N:expr)+) => { $({ type Array = [u8; $N]; - let array: Array = [0; $N]; + let mut array: Array = [0; $N]; let slice: &[u8] = &array[..]; let result = <&Array>::try_from(slice); assert_eq!(&array, result.unwrap()); + + let result = ::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 = ::try_from(mut_slice); + assert_eq!(&array, &result.unwrap()); })+ } }