diff --git a/src/vec.rs b/src/vec.rs index bd81c0f..1f6eaed 100644 --- a/src/vec.rs +++ b/src/vec.rs @@ -2,6 +2,8 @@ use alloc::alloc::Allocator; use alloc::collections::TryReserveError; use alloc::vec::Vec as InnerVec; use core::fmt::Debug; +use core::ops::{Index, IndexMut}; +use core::slice::SliceIndex; pub struct Vec { inner: InnerVec, @@ -71,6 +73,22 @@ impl Vec { } } +impl, A: Allocator> Index for Vec { + type Output = I::Output; + + #[inline] + fn index(&self, index: I) -> &Self::Output { + self.inner.index(index) + } +} + +impl, A: Allocator> IndexMut for Vec { + #[inline] + fn index_mut(&mut self, index: I) -> &mut Self::Output { + self.inner.index_mut(index) + } +} + impl Debug for Vec { fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { self.inner.fmt(f) @@ -255,4 +273,18 @@ mod tests { assert_eq!(*ptr.add(5), 'f'); } } + + #[test] + fn test_index() { + let wma = WatermarkAllocator::new(32); + let mut vec = Vec::new_in(wma); + vec.try_push(1).unwrap(); + vec.try_push(2).unwrap(); + vec.try_push(3).unwrap(); + vec.try_push(4).unwrap(); + assert_eq!(vec[0], 1); + assert_eq!(vec[1], 2); + assert_eq!(vec[2], 3); + assert_eq!(vec[3], 4); + } }