Skip to content
Open
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
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +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`.
- Implemented `Index` and `IndexMut` for `Vec`, `Deque`.

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

Expand Down
28 changes: 27 additions & 1 deletion src/deque.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ use core::{
iter::FusedIterator,
marker::PhantomData,
mem::{ManuallyDrop, MaybeUninit},
ptr, slice,
ops, ptr, slice,
};

#[cfg(feature = "zeroize")]
Expand Down Expand Up @@ -1364,6 +1364,22 @@ where

impl<T: Eq, const N: usize> Eq for Deque<T, N> {}

impl<T, S: VecStorage<T> + ?Sized> ops::Index<usize> for DequeInner<T, S> {
type Output = T;

#[inline]
fn index(&self, index: usize) -> &Self::Output {
self.get(index).expect("out of bounds access")
}
}

impl<T, S: VecStorage<T> + ?Sized> ops::IndexMut<usize> for DequeInner<T, S> {
#[inline]
fn index_mut(&mut self, index: usize) -> &mut Self::Output {
self.get_mut(index).expect("out of bounds access")
}
}

impl<T, const NS: usize, const ND: usize> TryFrom<[T; NS]> for Deque<T, ND> {
/// Converts a `[T; NS]` into a `Deque<T, ND>`.
///
Expand Down Expand Up @@ -1871,6 +1887,16 @@ mod tests {
assert_eq!(q.pop_front(), None);
}

#[test]
fn index() {
let mut q = Deque::<_, 4>::try_from([1, 2, 3]).unwrap();
assert_eq!(q[1], 2);
q[1] = 9;
assert_eq!(q[1], 9);
q[2] += 1;
assert_eq!(q[2], 4);
}

#[test]
fn swap() {
let mut q: Deque<i32, 4> = Deque::new();
Expand Down
4 changes: 2 additions & 2 deletions src/vec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1943,14 +1943,14 @@ mod tests {
increment(&mut v, 1);
assert_eq!(v[1], 10);

fn get<C>(container: &mut C, index: usize) -> &<C as core::ops::Index<usize>>::Output
fn get<C>(container: &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);
assert_eq!(*get(&v, 1), 10);
}

#[test]
Expand Down