From fb3a7c46f5e1593b8f84b833be2cb6e7ef9b6627 Mon Sep 17 00:00:00 2001 From: Tony Arcieri Date: Thu, 25 Jan 2024 15:00:29 -0700 Subject: [PATCH] Remove uses of `ArrayOps` bounds Continuation of the approach from #24 which replaces `ArrayOps` bounds with `ArraySize = [T; N]>`. Though a bit more verbose, this makes the inner type of `Array` explicit and therefore allows more safe conversions as well as making `unsafe` code easier to reason about, since the inner type is no longer implicit. This doesn't go as far as to remove the `ArrayOps` (and `SliceOps) trait(s) yet, but that should now be possible. --- src/lib.rs | 52 ++++++++++++++++++++++------------------------------ 1 file changed, 22 insertions(+), 30 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index d1db448..a6b8709 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -252,23 +252,22 @@ where impl AsRef<[T; N]> for Array where - Self: ArrayOps, - U: ArraySize, + U: ArraySize = [T; N]>, { #[inline] fn as_ref(&self) -> &[T; N] { - self.as_core_array() + &self.0 } } impl AsRef> for [T; N] where - Array: ArrayOps, - U: ArraySize, + U: ArraySize = [T; N]>, { #[inline] fn as_ref(&self) -> &Array { - Array::ref_from_core_array(self) + // SAFETY: `Self` is a `repr(transparent)` newtype for `[T; $len]` + unsafe { &*self.as_ptr().cast() } } } @@ -284,23 +283,22 @@ where impl AsMut<[T; N]> for Array where - Self: ArrayOps, - U: ArraySize, + U: ArraySize = [T; N]>, { #[inline] fn as_mut(&mut self) -> &mut [T; N] { - self.as_mut_core_array() + &mut self.0 } } impl AsMut> for [T; N] where - Array: ArrayOps, - U: ArraySize, + U: ArraySize = [T; N]>, { #[inline] fn as_mut(&mut self) -> &mut Array { - Array::ref_mut_from_core_array(self) + // SAFETY: `Self` is a `repr(transparent)` newtype for `[T; $len]` + unsafe { &mut *self.as_mut_ptr().cast() } } } @@ -316,12 +314,11 @@ where impl Borrow<[T; N]> for Array where - Self: ArrayOps, - U: ArraySize, + U: ArraySize = [T; N]>, { #[inline] fn borrow(&self) -> &[T; N] { - self.as_core_array() + &self.0 } } @@ -337,12 +334,11 @@ where impl BorrowMut<[T; N]> for Array where - Self: ArrayOps, - U: ArraySize, + U: ArraySize = [T; N]>, { #[inline] fn borrow_mut(&mut self) -> &mut [T; N] { - self.as_mut_core_array() + &mut self.0 } } @@ -437,45 +433,41 @@ where impl<'a, T, U, const N: usize> From<&'a [T; N]> for &'a Array where - Array: ArrayOps, - U: ArraySize, + U: ArraySize = [T; N]>, { #[inline] fn from(array_ref: &'a [T; N]) -> &'a Array { - >::ref_from_core_array(array_ref) + array_ref.as_ref() } } impl<'a, T, U, const N: usize> From<&'a Array> for &'a [T; N] where - Array: ArrayOps, - U: ArraySize, + U: ArraySize = [T; N]>, { #[inline] fn from(array_ref: &'a Array) -> &'a [T; N] { - array_ref.as_core_array() + array_ref.as_ref() } } impl<'a, T, U, const N: usize> From<&'a mut [T; N]> for &'a mut Array where - Array: ArrayOps, - U: ArraySize, + U: ArraySize = [T; N]>, { #[inline] fn from(array_ref: &'a mut [T; N]) -> &'a mut Array { - >::ref_mut_from_core_array(array_ref) + array_ref.as_mut() } } impl<'a, T, U, const N: usize> From<&'a mut Array> for &'a mut [T; N] where - Array: ArrayOps, - U: ArraySize, + U: ArraySize = [T; N]>, { #[inline] fn from(array_ref: &'a mut Array) -> &'a mut [T; N] { - array_ref.as_mut_core_array() + array_ref.as_mut() } }