Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 12 additions & 35 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,27 +157,6 @@ pub type ArrayN<T, const N: usize> = Array<T, <[T; N] as AssocArraySize>::Size>;
/// let arr: Array<u8, U3> = Array([1, 2, 3]);
/// ```
///
/// ## [`AsRef`] impls
///
/// The [`AsRef`] trait can be used to convert from `&Array<T, U>` to `&[T; N]` and vice versa:
///
/// ```
/// use hybrid_array::{Array, ArraySize, AssocArraySize, ArrayN, sizes::U3};
///
/// pub fn get_third_item_hybrid_array<T, U: ArraySize>(arr_ref: &Array<T, U>) -> &T {
/// &arr_ref[2]
/// }
///
/// pub fn get_third_item_const_generic<T, const N: usize>(arr_ref: &[T; N]) -> &T
/// where
/// [T; N]: AssocArraySize + AsRef<ArrayN<T, N>>
/// {
/// get_third_item_hybrid_array(arr_ref.as_ref())
/// }
Comment on lines -171 to -176
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the really sad thing to lose, BTW. It made it possible to write APIs using const generics that hid the use of Array behind the scenes, so the callers didn't have to care about Array at all

///
/// 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
Expand Down Expand Up @@ -486,15 +465,13 @@ where
}
}

impl<T, U, const N: usize> AsRef<Array<T, U>> for [T; N]
#[inline]
fn array_ref_as_hybrid_array_ref<T, U, const N: usize>(array_ref: &[T; N]) -> &Array<T, U>
where
U: ArraySize<ArrayType<T> = [T; N]>,
{
#[inline]
fn as_ref(&self) -> &Array<T, U> {
// 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<T, U> AsMut<[T]> for Array<T, U>
Expand All @@ -517,15 +494,15 @@ where
}
}

impl<T, U, const N: usize> AsMut<Array<T, U>> for [T; N]
#[inline]
fn array_mut_ref_as_hybrid_array_mut_ref<T, U, const N: usize>(
array_ref: &mut [T; N],
) -> &mut Array<T, U>
where
U: ArraySize<ArrayType<T> = [T; N]>,
{
#[inline]
fn as_mut(&mut self) -> &mut Array<T, U> {
// 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<T, U> Borrow<[T]> for Array<T, U>
Expand Down Expand Up @@ -663,7 +640,7 @@ where
{
#[inline]
fn from(array_ref: &'a [T; N]) -> &'a Array<T, U> {
array_ref.as_ref()
array_ref_as_hybrid_array_ref(array_ref)
}
}

Expand All @@ -683,7 +660,7 @@ where
{
#[inline]
fn from(array_ref: &'a mut [T; N]) -> &'a mut Array<T, U> {
array_ref.as_mut()
array_mut_ref_as_hybrid_array_mut_ref(array_ref)
}
}

Expand Down