From 72b411fd89675a38613d347b31481cd8e8a59bb7 Mon Sep 17 00:00:00 2001 From: rhysd Date: Sat, 20 Nov 2021 21:40:41 +0900 Subject: [PATCH 1/4] Implement `TryFrom<&'_ mut [T]>` for `[T; N]` --- library/core/src/array/mod.rs | 12 ++++++++++++ library/core/tests/array.rs | 13 ++++++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/library/core/src/array/mod.rs b/library/core/src/array/mod.rs index 09bb4519170da..eadf6fd9a4f89 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.57.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.as_ref()) + } +} + #[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()); })+ } } From 66e0523d09b01616e85565b35f97c99cc0bb2136 Mon Sep 17 00:00:00 2001 From: Linda_pp Date: Sat, 20 Nov 2021 23:35:28 +0900 Subject: [PATCH 2/4] Update version in `stable` attribute Co-authored-by: Joshua Nelson --- library/core/src/array/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/core/src/array/mod.rs b/library/core/src/array/mod.rs index eadf6fd9a4f89..700fa7f4e2d28 100644 --- a/library/core/src/array/mod.rs +++ b/library/core/src/array/mod.rs @@ -176,7 +176,7 @@ where } } -#[stable(feature = "try_from_mut_slice_to_array", since = "1.57.0")] +#[stable(feature = "try_from_mut_slice_to_array", since = "1.58.0")] impl TryFrom<&mut [T]> for [T; N] where T: Copy, From ac083c6b45bd3fbe0dc24ab57e2dd9d55f14d472 Mon Sep 17 00:00:00 2001 From: Linda_pp Date: Sun, 21 Nov 2021 11:01:31 +0900 Subject: [PATCH 3/4] Reborrow mut slice instead of converting it with `as_ref` Co-authored-by: Noah Lev --- library/core/src/array/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/core/src/array/mod.rs b/library/core/src/array/mod.rs index 700fa7f4e2d28..181f8b408e61b 100644 --- a/library/core/src/array/mod.rs +++ b/library/core/src/array/mod.rs @@ -184,7 +184,7 @@ where type Error = TryFromSliceError; fn try_from(slice: &mut [T]) -> Result<[T; N], TryFromSliceError> { - ::try_from(slice.as_ref()) + ::try_from(&*slice) } } From 16711fe0768d9df5daf73a2025913984ea17f8eb Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Sat, 4 Dec 2021 17:17:12 +0100 Subject: [PATCH 4/4] Update stabilization version of try_from_mut_slice_to_array --- library/core/src/array/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/core/src/array/mod.rs b/library/core/src/array/mod.rs index 181f8b408e61b..33f0f46a0a165 100644 --- a/library/core/src/array/mod.rs +++ b/library/core/src/array/mod.rs @@ -176,7 +176,7 @@ where } } -#[stable(feature = "try_from_mut_slice_to_array", since = "1.58.0")] +#[stable(feature = "try_from_mut_slice_to_array", since = "1.59.0")] impl TryFrom<&mut [T]> for [T; N] where T: Copy,