Skip to content
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "hybrid-array"
version = "0.3.1"
version = "0.4.0-pre"
description = """
Hybrid typenum-based and const generic array types designed to provide the
flexibility of typenum-based expressions while also allowing interoperability
Expand Down
54 changes: 4 additions & 50 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,32 +156,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())
/// }
///
/// 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
/// const generic size.
#[repr(transparent)]
pub struct Array<T, U: ArraySize>(pub U::ArrayType<T>);

Expand Down Expand Up @@ -486,17 +460,6 @@ where
}
}

impl<T, U, const N: usize> AsRef<Array<T, U>> for [T; N]
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() }
}
}

impl<T, U> AsMut<[T]> for Array<T, U>
where
U: ArraySize,
Expand All @@ -517,17 +480,6 @@ where
}
}

impl<T, U, const N: usize> AsMut<Array<T, U>> for [T; N]
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() }
}
}

impl<T, U> Borrow<[T]> for Array<T, U>
where
U: ArraySize,
Expand Down Expand Up @@ -663,7 +615,8 @@ where
{
#[inline]
fn from(array_ref: &'a [T; N]) -> &'a Array<T, U> {
array_ref.as_ref()
// SAFETY: `Self` is a `repr(transparent)` newtype for `[T; $len]`
unsafe { &*array_ref.as_ptr().cast() }
}
}

Expand All @@ -683,7 +636,8 @@ where
{
#[inline]
fn from(array_ref: &'a mut [T; N]) -> &'a mut Array<T, U> {
array_ref.as_mut()
// SAFETY: `Self` is a `repr(transparent)` newtype for `[T; $len]`
unsafe { &mut *array_ref.as_mut_ptr().cast() }
}
}

Expand Down