From 1fae762dce065ba3384bda2c22f8a696856afb0c Mon Sep 17 00:00:00 2001 From: est31 Date: Sat, 4 Mar 2023 08:46:15 +0100 Subject: [PATCH] Impl Deref/DerefMut for Array --- src/array.rs | 16 +++++++++++++++- tests/nested.rs | 1 + 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/array.rs b/src/array.rs index 6604485..78cd367 100644 --- a/src/array.rs +++ b/src/array.rs @@ -1,5 +1,5 @@ use crate::BigArray; -use core::ops::{Index, IndexMut}; +use core::ops::{Deref, DerefMut, Index, IndexMut}; use serde::{Deserialize, Deserializer, Serialize, Serializer}; /// An array newtype usable for nested structures @@ -42,6 +42,20 @@ impl Serialize for Array { } } +impl Deref for Array { + type Target = [T; N]; + + fn deref(&self) -> &Self::Target { + &self.0 + } +} + +impl DerefMut for Array { + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.0 + } +} + impl Index for Array where [T]: Index, diff --git a/tests/nested.rs b/tests/nested.rs index 0ffc597..9a5263b 100644 --- a/tests/nested.rs +++ b/tests/nested.rs @@ -16,4 +16,5 @@ fn test() { let j = serde_json::to_string(&s).unwrap(); let s_back = serde_json::from_str::(&j).unwrap(); assert!(&s.arr[..] == &s_back.arr[..]); + assert_eq!(s.arr.len(), 65); }