From 8daf7d7096f9e4fe1781d35dac9748d6169df059 Mon Sep 17 00:00:00 2001 From: Luke Parker Date: Fri, 29 Aug 2025 04:56:45 -0400 Subject: [PATCH 1/2] Remove AsRef for [T; N] --- src/lib.rs | 33 +++++---------------------------- 1 file changed, 5 insertions(+), 28 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index eb4cf22..16c3af9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -157,27 +157,6 @@ pub type ArrayN = Array::Size>; /// let arr: Array = Array([1, 2, 3]); /// ``` /// -/// ## [`AsRef`] impls -/// -/// The [`AsRef`] trait can be used to convert from `&Array` to `&[T; N]` and vice versa: -/// -/// ``` -/// use hybrid_array::{Array, ArraySize, AssocArraySize, ArrayN, sizes::U3}; -/// -/// pub fn get_third_item_hybrid_array(arr_ref: &Array) -> &T { -/// &arr_ref[2] -/// } -/// -/// pub fn get_third_item_const_generic(arr_ref: &[T; N]) -> &T -/// where -/// [T; N]: AssocArraySize + AsRef> -/// { -/// get_third_item_hybrid_array(arr_ref.as_ref()) -/// } -/// -/// assert_eq!(get_third_item_const_generic(&[1u8, 2, 3, 4]), &3); -/// ``` -/// /// Note that the [`AssocArraySize`] trait can be used to determine the appropriate /// [`Array`] size for a given `[T; N]`, and the [`ArrayN`] trait (which internally uses /// [`AssocArraySize`]) can be used to determine the specific [`Array`] type for a given @@ -486,15 +465,13 @@ where } } -impl AsRef> for [T; N] +#[inline] +fn array_ref_as_hybrid_array_ref(array: &[T; N]) -> &Array where U: ArraySize = [T; N]>, { - #[inline] - fn as_ref(&self) -> &Array { - // SAFETY: `Self` is a `repr(transparent)` newtype for `[T; $len]` - unsafe { &*self.as_ptr().cast() } - } + // SAFETY: `Self` is a `repr(transparent)` newtype for `[T; $len]` + unsafe { &*array.as_ptr().cast() } } impl AsMut<[T]> for Array @@ -663,7 +640,7 @@ where { #[inline] fn from(array_ref: &'a [T; N]) -> &'a Array { - array_ref.as_ref() + array_ref_as_hybrid_array_ref(array_ref) } } From 8caa508976c93696a67f40734537c91be7cecd96 Mon Sep 17 00:00:00 2001 From: Luke Parker Date: Fri, 29 Aug 2025 05:02:10 -0400 Subject: [PATCH 2/2] Remove AsMut for [T; N] --- src/lib.rs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 16c3af9..f3c0b4b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -466,12 +466,12 @@ where } #[inline] -fn array_ref_as_hybrid_array_ref(array: &[T; N]) -> &Array +fn array_ref_as_hybrid_array_ref(array_ref: &[T; N]) -> &Array where U: ArraySize = [T; N]>, { // SAFETY: `Self` is a `repr(transparent)` newtype for `[T; $len]` - unsafe { &*array.as_ptr().cast() } + unsafe { &*array_ref.as_ptr().cast() } } impl AsMut<[T]> for Array @@ -494,15 +494,15 @@ where } } -impl AsMut> for [T; N] +#[inline] +fn array_mut_ref_as_hybrid_array_mut_ref( + array_ref: &mut [T; N], +) -> &mut Array where U: ArraySize = [T; N]>, { - #[inline] - fn as_mut(&mut self) -> &mut Array { - // SAFETY: `Self` is a `repr(transparent)` newtype for `[T; $len]` - unsafe { &mut *self.as_mut_ptr().cast() } - } + // SAFETY: `Self` is a `repr(transparent)` newtype for `[T; $len]` + unsafe { &mut *array_ref.as_mut_ptr().cast() } } impl Borrow<[T]> for Array @@ -660,7 +660,7 @@ where { #[inline] fn from(array_ref: &'a mut [T; N]) -> &'a mut Array { - array_ref.as_mut() + array_mut_ref_as_hybrid_array_mut_ref(array_ref) } }