diff --git a/src/lib.rs b/src/lib.rs index eb4cf22..f3c0b4b 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_ref: &[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_ref.as_ptr().cast() } } impl AsMut<[T]> for Array @@ -517,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 @@ -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) } } @@ -683,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) } }