Skip to content

Commit

Permalink
hybrid-array: add Deref/DerefMut impls for Array (#908)
Browse files Browse the repository at this point in the history
Derefs to the inner core array type.

Also adds bounds for `Deref`/`DerefMut` to `ArrayOps`, with a `Target`
of `[T; N]`.
  • Loading branch information
tarcieri authored May 30, 2023
1 parent ee1c0e3 commit 6813b81
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion hybrid-array/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub use typenum;
use core::{
array::{IntoIter, TryFromSliceError},
borrow::{Borrow, BorrowMut},
ops::{Index, IndexMut, Range},
ops::{Deref, DerefMut, Index, IndexMut, Range},
slice::{Iter, IterMut},
};
use typenum::Unsigned;
Expand Down Expand Up @@ -151,6 +151,26 @@ where
}
}

impl<T, U> Deref for Array<T, U>
where
U: ArraySize,
{
type Target = U::ArrayType<T>;

fn deref(&self) -> &U::ArrayType<T> {
&self.0
}
}

impl<T, U> DerefMut for Array<T, U>
where
U: ArraySize,
{
fn deref_mut(&mut self) -> &mut U::ArrayType<T> {
&mut self.0
}
}

impl<T, U, const N: usize> From<[T; N]> for Array<T, U>
where
Self: ArrayOps<T, N>,
Expand Down Expand Up @@ -273,6 +293,8 @@ pub trait ArrayOps<T, const N: usize>:
+ AsMut<[T; N]>
+ Borrow<[T; N]>
+ BorrowMut<[T; N]>
+ Deref<Target = [T; N]>
+ DerefMut
+ From<[T; N]>
+ Index<usize>
+ Index<Range<usize>>
Expand Down

0 comments on commit 6813b81

Please sign in to comment.