diff --git a/src/lib.rs b/src/lib.rs index 0570e287e..c9c55678b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -687,7 +687,7 @@ impl GenericArray { /// Extracts a mutable slice containing the entire array. #[inline(always)] - pub fn as_mut_slice(&mut self) -> &mut [T] { + pub const fn as_mut_slice(&mut self) -> &mut [T] { unsafe { slice::from_raw_parts_mut(self as *mut Self as *mut T, N::USIZE) } } @@ -729,10 +729,9 @@ impl GenericArray { /// /// Consider [`TryFrom`]/[`TryInto`] for a fallible conversion. #[inline(always)] - pub fn from_mut_slice(slice: &mut [T]) -> &mut GenericArray { - assert_eq!( - slice.len(), - N::USIZE, + pub const fn from_mut_slice(slice: &mut [T]) -> &mut GenericArray { + assert!( + slice.len() == N::USIZE, "slice.len() != N in GenericArray::from_mut_slice" ); @@ -745,8 +744,11 @@ impl GenericArray { /// and current just calls [`TryFrom`] internally, but is provided for /// future compatibility when we can make it const. #[inline(always)] - pub fn try_from_mut_slice(slice: &mut [T]) -> Result<&mut GenericArray, LengthError> { - TryFrom::try_from(slice) + pub const fn try_from_mut_slice(slice: &mut [T]) -> Result<&mut GenericArray, LengthError> { + match slice.len() == N::USIZE { + true => Ok(GenericArray::from_mut_slice(slice)), + false => Err(LengthError), + } } /// Converts a slice of `T` elements into a slice of `GenericArray` chunks. @@ -782,7 +784,7 @@ impl GenericArray { /// # Panics /// /// Panics if `N` is `U0` _AND_ the input slice is not empty. - pub fn chunks_from_slice_mut(slice: &mut [T]) -> (&mut [GenericArray], &mut [T]) { + pub const fn chunks_from_slice_mut(slice: &mut [T]) -> (&mut [GenericArray], &mut [T]) { if N::USIZE == 0 { assert!(slice.is_empty(), "GenericArray length N must be non-zero"); return (&mut [], &mut []); @@ -812,7 +814,7 @@ impl GenericArray { /// Convert a slice of `GenericArray` into a slice of `T`, effectively flattening the arrays. #[inline(always)] - pub fn slice_from_chunks_mut(slice: &mut [GenericArray]) -> &mut [T] { + pub const fn slice_from_chunks_mut(slice: &mut [GenericArray]) -> &mut [T] { unsafe { slice::from_raw_parts_mut(slice.as_mut_ptr() as *mut T, slice.len() * N::USIZE) } } @@ -849,7 +851,7 @@ impl GenericArray { /// Convert a mutable slice of native arrays into a mutable slice of `GenericArray`s. #[inline(always)] - pub fn from_chunks_mut(chunks: &mut [[T; U]]) -> &mut [GenericArray] + pub const fn from_chunks_mut(chunks: &mut [[T; U]]) -> &mut [GenericArray] where Const: IntoArrayLength, { @@ -867,7 +869,7 @@ impl GenericArray { /// Converts a mutable slice `GenericArray` into a mutable slice of `[T; N]` #[inline(always)] - pub fn into_chunks_mut(chunks: &mut [GenericArray]) -> &mut [[T; U]] + pub const fn into_chunks_mut(chunks: &mut [GenericArray]) -> &mut [[T; U]] where Const: IntoArrayLength, { @@ -943,10 +945,7 @@ impl<'a, T, N: ArrayLength> TryFrom<&'a mut [T]> for &'a mut GenericArray #[inline(always)] fn try_from(slice: &'a mut [T]) -> Result { - match slice.len() == N::USIZE { - true => Ok(GenericArray::from_mut_slice(slice)), - false => Err(LengthError), - } + GenericArray::try_from_mut_slice(slice) } }