Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,10 @@ use core::convert::TryInto;
use core::hash::*;
use core::iter::FromIterator;
use core::marker::PhantomData;
use core::ops::Bound;
use core::ops::{Bound, Index, IndexMut};
use core::ops::{Deref, DerefMut, RangeBounds};
use core::ptr::NonNull;
use core::slice::Iter;
use core::slice::{Iter, SliceIndex};
use core::{fmt, mem, ops, ptr, slice};

use impl_details::*;
Expand Down Expand Up @@ -2078,6 +2078,20 @@ impl<T> DerefMut for ThinVec<T> {
}
}

impl<T, I: SliceIndex<[T]>> Index<I> for ThinVec<T> {
type Output = <I as SliceIndex<[T]>>::Output;

fn index(&self, index: I) -> &Self::Output {
&self.deref()[index]
}
}

impl<T, I: SliceIndex<[T]>> IndexMut<I> for ThinVec<T> {
fn index_mut(&mut self, index: I) -> &mut Self::Output {
&mut self.deref_mut()[index]
}
}

impl<T> Borrow<[T]> for ThinVec<T> {
fn borrow(&self) -> &[T] {
self.as_slice()
Expand Down Expand Up @@ -4046,6 +4060,13 @@ mod std_tests {
assert!(vec[1] == 2);
}

#[test]
fn test_index_mut() {
let mut vec = thin_vec![1, 2, 3];
vec[1] = 22;
assert!(vec[1] == 22);
}

#[test]
#[should_panic]
fn test_index_out_of_bounds() {
Expand Down