Skip to content

Commit 6813b81

Browse files
authored
hybrid-array: add Deref/DerefMut impls for Array (#908)
Derefs to the inner core array type. Also adds bounds for `Deref`/`DerefMut` to `ArrayOps`, with a `Target` of `[T; N]`.
1 parent ee1c0e3 commit 6813b81

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

hybrid-array/src/lib.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ pub use typenum;
2828
use core::{
2929
array::{IntoIter, TryFromSliceError},
3030
borrow::{Borrow, BorrowMut},
31-
ops::{Index, IndexMut, Range},
31+
ops::{Deref, DerefMut, Index, IndexMut, Range},
3232
slice::{Iter, IterMut},
3333
};
3434
use typenum::Unsigned;
@@ -151,6 +151,26 @@ where
151151
}
152152
}
153153

154+
impl<T, U> Deref for Array<T, U>
155+
where
156+
U: ArraySize,
157+
{
158+
type Target = U::ArrayType<T>;
159+
160+
fn deref(&self) -> &U::ArrayType<T> {
161+
&self.0
162+
}
163+
}
164+
165+
impl<T, U> DerefMut for Array<T, U>
166+
where
167+
U: ArraySize,
168+
{
169+
fn deref_mut(&mut self) -> &mut U::ArrayType<T> {
170+
&mut self.0
171+
}
172+
}
173+
154174
impl<T, U, const N: usize> From<[T; N]> for Array<T, U>
155175
where
156176
Self: ArrayOps<T, N>,
@@ -273,6 +293,8 @@ pub trait ArrayOps<T, const N: usize>:
273293
+ AsMut<[T; N]>
274294
+ Borrow<[T; N]>
275295
+ BorrowMut<[T; N]>
296+
+ Deref<Target = [T; N]>
297+
+ DerefMut
276298
+ From<[T; N]>
277299
+ Index<usize>
278300
+ Index<Range<usize>>

0 commit comments

Comments
 (0)