Skip to content

Commit bab82ae

Browse files
committed
Implement Index and IndexMut traits for ArrayVec
1 parent e5b3c9b commit bab82ae

File tree

1 file changed

+42
-6
lines changed

1 file changed

+42
-6
lines changed

src/arrayvec.rs

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
use std::cmp;
33
use std::iter;
44
use std::mem;
5-
use std::ops::{Bound, Deref, DerefMut, RangeBounds};
5+
use std::ops::{Bound, Deref, DerefMut, Index, IndexMut, RangeBounds};
66
use std::ptr;
77
use std::slice;
88

@@ -373,7 +373,7 @@ impl<T, const CAP: usize> ArrayVec<T, CAP> {
373373

374374
/// Remove the element at `index` and swap the last element into its place.
375375
///
376-
/// This is a checked version of `.swap_remove`.
376+
/// This is a checked version of `.swap_remove`.
377377
/// This operation is O(1).
378378
///
379379
/// Return `Some(` *element* `)` if the index is in bounds, else `None`.
@@ -989,11 +989,11 @@ impl<T, Data, F> Drop for ScopeExitGuard<T, Data, F>
989989

990990

991991
/// Extend the `ArrayVec` with an iterator.
992-
///
992+
///
993993
/// ***Panics*** if extending the vector exceeds its capacity.
994994
impl<T, const CAP: usize> Extend<T> for ArrayVec<T, CAP> {
995995
/// Extend the `ArrayVec` with an iterator.
996-
///
996+
///
997997
/// ***Panics*** if extending the vector exceeds its capacity.
998998
fn extend<I: IntoIterator<Item=T>>(&mut self, iter: I) {
999999
unsafe {
@@ -1002,6 +1002,42 @@ impl<T, const CAP: usize> Extend<T> for ArrayVec<T, CAP> {
10021002
}
10031003
}
10041004

1005+
/// Perform the indexing (`container[index]`) operation.
1006+
///
1007+
/// # Panics
1008+
/// Panics if index is out of bounds.
1009+
impl<I, T, const CAP: usize> Index<I> for ArrayVec<T, CAP>
1010+
where
1011+
I: slice::SliceIndex<[T]>,
1012+
{
1013+
type Output = <I as slice::SliceIndex<[T]>>::Output;
1014+
1015+
/// Performs the indexing (`container[index]`) operation.
1016+
///
1017+
/// # Panics
1018+
/// Panics if index is out of bounds.
1019+
fn index(&self, index: I) -> &Self::Output {
1020+
self.get(index).unwrap()
1021+
}
1022+
}
1023+
1024+
/// Perform the mutable indexing (`container[index]`) operation.
1025+
///
1026+
/// # Panics
1027+
/// Panics if index is out of bounds.
1028+
impl<I, T, const CAP: usize> IndexMut<I> for ArrayVec<T, CAP>
1029+
where
1030+
I: slice::SliceIndex<[T]>,
1031+
{
1032+
/// Perform the mutable indexing (`container[index]`) operation.
1033+
///
1034+
/// # Panics
1035+
/// Panics if index is out of bounds.
1036+
fn index_mut(&mut self, index: I) -> &mut Self::Output {
1037+
self.get_mut(index).unwrap()
1038+
}
1039+
}
1040+
10051041
#[inline(never)]
10061042
#[cold]
10071043
fn extend_panic() {
@@ -1072,11 +1108,11 @@ unsafe fn raw_ptr_add<T>(ptr: *mut T, offset: usize) -> *mut T {
10721108
}
10731109

10741110
/// Create an `ArrayVec` from an iterator.
1075-
///
1111+
///
10761112
/// ***Panics*** if the number of elements in the iterator exceeds the arrayvec's capacity.
10771113
impl<T, const CAP: usize> iter::FromIterator<T> for ArrayVec<T, CAP> {
10781114
/// Create an `ArrayVec` from an iterator.
1079-
///
1115+
///
10801116
/// ***Panics*** if the number of elements in the iterator exceeds the arrayvec's capacity.
10811117
fn from_iter<I: IntoIterator<Item=T>>(iter: I) -> Self {
10821118
let mut array = ArrayVec::new();

0 commit comments

Comments
 (0)