Skip to content
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Limited max size of `IndexMap` to u16::MAX + 1.
The implementation for sizes higher than u16::MAX were unsound anyway.
- Added `shift_remove`, `shift_remove_entry` and `shift_remove_full` to `IndexMap` and `IndexSet`.
- Implemented `Index` and `IndexMut` for `Vec`.

## [v0.9.3] 2025-04-15

Expand Down
53 changes: 53 additions & 0 deletions src/vec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1813,6 +1813,28 @@ where
}
}

impl<T, I, LenT: LenType, S: VecStorage<T> + ?Sized> ops::Index<I> for VecInner<T, LenT, S>
where
I: slice::SliceIndex<[T]>,
{
type Output = I::Output;

#[inline]
fn index(&self, index: I) -> &Self::Output {
ops::Index::index(self.as_slice(), index)
}
}

impl<T, I, LenT: LenType, S: VecStorage<T> + ?Sized> ops::IndexMut<I> for VecInner<T, LenT, S>
where
I: slice::SliceIndex<[T]>,
{
#[inline]
fn index_mut(&mut self, index: I) -> &mut Self::Output {
ops::IndexMut::index_mut(self.as_mut_slice(), index)
}
}

impl<T, LenT: LenType, S: VecStorage<T> + ?Sized> ops::Deref for VecInner<T, LenT, S> {
type Target = [T];

Expand Down Expand Up @@ -1900,6 +1922,37 @@ mod tests {
let mut _v: Vec<i32, 4> = Vec::new();
}

#[test]
fn index() {
let mut v: Vec<i32, 4> = Vec::from_array([1, 2, 3]);
assert_eq!(v[1], 2);
v[1] = 9;
assert_eq!(v[1], 9);

// Now we test if code using `Index` and `IndexMut` trait bounds
// actually works.

fn increment<C>(container: &mut C, index: usize)
where
C: std::ops::IndexMut<usize>,
C::Output: std::ops::AddAssign<i32>,
{
container[index] += 1;
}

increment(&mut v, 1);
assert_eq!(v[1], 10);

fn get<C>(container: &mut C, index: usize) -> &<C as core::ops::Index<usize>>::Output
where
C: std::ops::Index<usize>,
{
&container[index]
}

assert_eq!(*get(&mut v, 1), 10);
}

Comment thread
mikwielgus marked this conversation as resolved.
Resolved
#[test]
fn is_full_empty() {
let mut v: Vec<i32, 4> = Vec::new();
Expand Down
Loading