Skip to content

Commit

Permalink
Impl Deref/DerefMut for Array
Browse files Browse the repository at this point in the history
  • Loading branch information
est31 committed Mar 4, 2023
1 parent a5ac7b2 commit 1fae762
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/array.rs
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -42,6 +42,20 @@ impl<T: Serialize, const N: usize> Serialize for Array<T, N> {
}
}

impl<T, const N: usize> Deref for Array<T, N> {
type Target = [T; N];

fn deref(&self) -> &Self::Target {
&self.0
}
}

impl<T, const N: usize> DerefMut for Array<T, N> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}

impl<T, I, const N: usize> Index<I> for Array<T, N>
where
[T]: Index<I>,
Expand Down
1 change: 1 addition & 0 deletions tests/nested.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ fn test() {
let j = serde_json::to_string(&s).unwrap();
let s_back = serde_json::from_str::<S>(&j).unwrap();
assert!(&s.arr[..] == &s_back.arr[..]);
assert_eq!(s.arr.len(), 65);
}

0 comments on commit 1fae762

Please sign in to comment.