diff --git a/src/references.rs b/src/references.rs index 0582ea12..06b4a381 100644 --- a/src/references.rs +++ b/src/references.rs @@ -10,8 +10,11 @@ use { }, ops::{ Deref, - DerefMut - } + DerefMut, + Index, + IndexMut + }, + slice::SliceIndex } }; @@ -55,3 +58,19 @@ impl DerefMut for SmallVec { self.as_mut_slice() } } + +impl> Index for SmallVec { + type Output = >::Output; + + #[inline] + fn index(&self, index: I) -> &Self::Output { + &self.deref()[index] + } +} + +impl> IndexMut for SmallVec { + #[inline] + fn index_mut(&mut self, index: I) -> &mut Self::Output { + &mut self.deref_mut()[index] + } +} diff --git a/tests/main.rs b/tests/main.rs index 0c230d87..d58fcc02 100644 --- a/tests/main.rs +++ b/tests/main.rs @@ -629,6 +629,19 @@ fn borrow_mut() { assert_eq!(a.borrow_mut(), [1, 4, 3]); } +#[test] +fn index() { + let a = SmallVec::::from(&[1, 2, 3][..]); + assert_eq!(a[1], 2); +} + +#[test] +fn index_mut() { + let mut a = SmallVec::::from(&[1, 2, 3][..]); + a[1] = 22; + assert_eq!(a[1], 22); +} + #[test] fn from() { assert_eq!(&SmallVec::::from(&[1][..])[..], [1]);