Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Index and IndexMut traits for ArrayVec #191

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
48 changes: 42 additions & 6 deletions src/arrayvec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use std::cmp;
use std::iter;
use std::mem;
use std::ops::{Bound, Deref, DerefMut, RangeBounds};
use std::ops::{Bound, Deref, DerefMut, Index, IndexMut, RangeBounds};
use std::ptr;
use std::slice;

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

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


/// Extend the `ArrayVec` with an iterator.
///
///
/// ***Panics*** if extending the vector exceeds its capacity.
impl<T, const CAP: usize> Extend<T> for ArrayVec<T, CAP> {
/// Extend the `ArrayVec` with an iterator.
///
///
/// ***Panics*** if extending the vector exceeds its capacity.
fn extend<I: IntoIterator<Item=T>>(&mut self, iter: I) {
unsafe {
Expand All @@ -1002,6 +1002,42 @@ impl<T, const CAP: usize> Extend<T> for ArrayVec<T, CAP> {
}
}

/// Perform the indexing (`container[index]`) operation.
///
/// # Panics
/// Panics if index is out of bounds.
impl<I, T, const CAP: usize> Index<I> for ArrayVec<T, CAP>
where
I: slice::SliceIndex<[T]>,
{
type Output = <I as slice::SliceIndex<[T]>>::Output;

/// Performs the indexing (`container[index]`) operation.
///
/// # Panics
/// Panics if index is out of bounds.
fn index(&self, index: I) -> &Self::Output {
self.deref().index(index)
niluxv marked this conversation as resolved.
Show resolved Hide resolved
}
}

/// Perform the mutable indexing (`container[index]`) operation.
///
/// # Panics
/// Panics if index is out of bounds.
impl<I, T, const CAP: usize> IndexMut<I> for ArrayVec<T, CAP>
where
I: slice::SliceIndex<[T]>,
{
/// Perform the mutable indexing (`container[index]`) operation.
///
/// # Panics
/// Panics if index is out of bounds.
fn index_mut(&mut self, index: I) -> &mut Self::Output {
self.deref_mut().index_mut(index)
niluxv marked this conversation as resolved.
Show resolved Hide resolved
}
}

#[inline(never)]
#[cold]
fn extend_panic() {
Expand Down Expand Up @@ -1072,11 +1108,11 @@ unsafe fn raw_ptr_add<T>(ptr: *mut T, offset: usize) -> *mut T {
}

/// Create an `ArrayVec` from an iterator.
///
///
/// ***Panics*** if the number of elements in the iterator exceeds the arrayvec's capacity.
impl<T, const CAP: usize> iter::FromIterator<T> for ArrayVec<T, CAP> {
/// Create an `ArrayVec` from an iterator.
///
///
/// ***Panics*** if the number of elements in the iterator exceeds the arrayvec's capacity.
fn from_iter<I: IntoIterator<Item=T>>(iter: I) -> Self {
let mut array = ArrayVec::new();
Expand Down
40 changes: 40 additions & 0 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,46 @@ fn array_clone_from() {
assert_eq!(&t, &reference[..]);
}

#[test]
fn array_index_usize() {
let v = ArrayVec::from([1, 2, 3]);
assert_eq!(v[1], 2);
}

#[should_panic(expected="index out of bounds: the len is 3 but the index is 3")]
#[test]
fn array_index_usize_out_of_bounds() {
let v = ArrayVec::from([1, 2, 3]);
let _ = v[3]; // out of bounds
}

#[test]
fn array_index_range() {
let v = ArrayVec::from([1, 2, 3]);
assert_eq!(&v[1..3], [2, 3].as_ref());
}

#[should_panic(expected="range end index 4 out of range for slice of length 3")]
#[test]
fn array_index_range_out_of_bounds() {
let v = ArrayVec::from([1, 2, 3]);
let _ = v[1..4]; // out of bounds
}

#[test]
fn array_indexmut_usize() {
let mut v = ArrayVec::from([1, 2, 3]);
v[1] = 0;
assert_eq!(v, ArrayVec::from([1, 0, 3]));
}

#[should_panic(expected="index out of bounds: the len is 3 but the index is 3")]
#[test]
fn array_indexmut_usize_out_of_bounds() {
let mut v = ArrayVec::from([1, 2, 3]);
v[3] = 0; // out of bounds
}

#[cfg(feature="std")]
#[test]
fn test_string() {
Expand Down