Skip to content

Commit

Permalink
hybrid-array: use From for Array<->[T; N] reference conversions (
Browse files Browse the repository at this point in the history
…#1026)

...instead of `AsRef`/`AsMut`, which allows inference to work for those
traits.

Notably `[T; N]` doesn't impl `AsRef`/`AsMut` for itself, only for
slices, so this is more consistent.
  • Loading branch information
tarcieri authored Dec 31, 2023
1 parent b45f35f commit 404a67e
Showing 1 changed file with 22 additions and 22 deletions.
44 changes: 22 additions & 22 deletions hybrid-array/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,17 +212,6 @@ where
}
}

impl<T, U, const N: usize> AsRef<[T; N]> for Array<T, U>
where
Self: ArrayOps<T, N>,
U: ArraySize,
{
#[inline]
fn as_ref(&self) -> &[T; N] {
self.as_core_array()
}
}

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

impl<T, U, const N: usize> AsMut<[T; N]> for Array<T, U>
where
Self: ArrayOps<T, N>,
U: ArraySize,
{
#[inline]
fn as_mut(&mut self) -> &mut [T; N] {
self.as_mut_core_array()
}
}

impl<T, U> Borrow<[T]> for Array<T, U>
where
U: ArraySize,
Expand Down Expand Up @@ -386,6 +364,17 @@ where
}
}

impl<'a, T, U, const N: usize> From<&'a Array<T, U>> for &'a [T; N]
where
Array<T, U>: ArrayOps<T, N>,
U: ArraySize,
{
#[inline]
fn from(array_ref: &'a Array<T, U>) -> &'a [T; N] {
array_ref.as_core_array()
}
}

impl<'a, T, U, const N: usize> From<&'a mut [T; N]> for &'a mut Array<T, U>
where
Array<T, U>: ArrayOps<T, N>,
Expand All @@ -397,6 +386,17 @@ where
}
}

impl<'a, T, U, const N: usize> From<&'a mut Array<T, U>> for &'a mut [T; N]
where
Array<T, U>: ArrayOps<T, N>,
U: ArraySize,
{
#[inline]
fn from(array_ref: &'a mut Array<T, U>) -> &'a mut [T; N] {
array_ref.as_mut_core_array()
}
}

impl<T, U> Hash for Array<T, U>
where
T: Hash,
Expand Down

0 comments on commit 404a67e

Please sign in to comment.