From 3738a35cc91cf6ee3b1ea024446ea7863787aec2 Mon Sep 17 00:00:00 2001 From: Tony Arcieri Date: Tue, 30 May 2023 09:31:16 -0600 Subject: [PATCH] hybrid-array: add `Deref`/`DerefMut` impls for `Array` Derefs to the inner core array type. Also adds bounds for `Deref`/`DerefMut` to `ArrayOps`, with a `Target` of `[T; N]`. --- hybrid-array/src/lib.rs | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/hybrid-array/src/lib.rs b/hybrid-array/src/lib.rs index c2c34732..36e439fb 100644 --- a/hybrid-array/src/lib.rs +++ b/hybrid-array/src/lib.rs @@ -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; @@ -151,6 +151,26 @@ where } } +impl Deref for Array +where + U: ArraySize, +{ + type Target = U::ArrayType; + + fn deref(&self) -> &U::ArrayType { + &self.0 + } +} + +impl DerefMut for Array +where + U: ArraySize, +{ + fn deref_mut(&mut self) -> &mut U::ArrayType { + &mut self.0 + } +} + impl From<[T; N]> for Array where Self: ArrayOps, @@ -273,6 +293,8 @@ pub trait ArrayOps: + AsMut<[T; N]> + Borrow<[T; N]> + BorrowMut<[T; N]> + + Deref + + DerefMut + From<[T; N]> + Index + Index>