diff --git a/eth2/utils/fixed_len_vec/src/lib.rs b/eth2/utils/fixed_len_vec/src/lib.rs index 2976ee4e4..5066db288 100644 --- a/eth2/utils/fixed_len_vec/src/lib.rs +++ b/eth2/utils/fixed_len_vec/src/lib.rs @@ -1,6 +1,6 @@ use serde_derive::{Deserialize, Serialize}; use std::marker::PhantomData; -use std::ops::{Index, IndexMut}; +use std::ops::{Deref, Index, IndexMut}; use std::slice::SliceIndex; use typenum::Unsigned; @@ -70,6 +70,14 @@ impl> IndexMut for FixedLenVec { } } +impl Deref for FixedLenVec { + type Target = [T]; + + fn deref(&self) -> &[T] { + &self.vec[..] + } +} + #[cfg(test)] mod test { use super::*; @@ -104,6 +112,16 @@ mod test { let fixed: FixedLenVec = FixedLenVec::from(vec.clone()); assert_eq!(&fixed[..], &vec![0, 0, 0, 0][..]); } + + #[test] + fn deref() { + let vec = vec![0, 2, 4, 6]; + let fixed: FixedLenVec = FixedLenVec::from(vec); + + assert_eq!(fixed.get(0), Some(&0)); + assert_eq!(fixed.get(3), Some(&6)); + assert_eq!(fixed.get(4), None); + } } #[cfg(test)]