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
23 changes: 14 additions & 9 deletions src/arrayvec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ impl<T, const CAP: usize> ArrayVec<T, CAP> {
///
/// Returns an error if vector is already at full capacity.
///
/// ***Panics*** `index` is out of bounds.
/// ***Panics*** if the `index` is out of bounds.
///
/// ```
/// use arrayvec::ArrayVec;
Expand All @@ -306,6 +306,7 @@ impl<T, const CAP: usize> ArrayVec<T, CAP> {
/// assert_eq!(&array[..], &["y", "x"]);
///
/// ```
#[track_caller]
pub fn try_insert(&mut self, index: usize, element: T) -> Result<(), CapacityError<T>> {
if index > self.len() {
panic_oob!("try_insert", index, self.len())
Expand Down Expand Up @@ -369,11 +370,13 @@ impl<T, const CAP: usize> ArrayVec<T, CAP> {
/// assert_eq!(array.swap_remove(1), 2);
/// assert_eq!(&array[..], &[3]);
/// ```
#[track_caller]
pub fn swap_remove(&mut self, index: usize) -> T {
self.swap_pop(index)
.unwrap_or_else(|| {
panic_oob!("swap_remove", index, self.len())
})
let len = self.len();
match self.swap_pop(index) {
Some(element) => element,
None => panic_oob!("swap_remove", index, len),
}
}

/// Remove the element at `index` and swap the last element into its place.
Expand Down Expand Up @@ -417,11 +420,13 @@ impl<T, const CAP: usize> ArrayVec<T, CAP> {
/// assert_eq!(removed_elt, 1);
/// assert_eq!(&array[..], &[2, 3]);
/// ```
#[track_caller]
pub fn remove(&mut self, index: usize) -> T {
self.pop_at(index)
.unwrap_or_else(|| {
panic_oob!("remove", index, self.len())
})
let len = self.len();
match self.pop_at(index) {
Some(element) => element,
None => panic_oob!("remove", index, len),
}
}

/// Remove the element at `index` and shift down the following elements.
Expand Down
50 changes: 50 additions & 0 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -821,3 +821,53 @@ fn test_arraystring_zero_filled_has_some_sanity_checks() {
assert_eq!(string.as_str(), "\0\0\0\0");
assert_eq!(string.len(), 4);
}

#[test]
fn test_track_caller_out_of_bounds() {
// The out-of-bounds panics of `insert`/`try_insert`/`remove`/`swap_remove`
// must report the caller's location, not a line inside arrayvec.
use std::panic::{self, AssertUnwindSafe};
use std::sync::{Arc, Mutex};

fn panic_file<F: FnOnce()>(f: F) -> String {
let slot = Arc::new(Mutex::new(String::new()));
let sink = Arc::clone(&slot);
let prev = panic::take_hook();
panic::set_hook(Box::new(move |info| {
if let Some(loc) = info.location() {
*sink.lock().unwrap() = loc.file().to_string();
}
}));
let result = panic::catch_unwind(AssertUnwindSafe(f));
panic::set_hook(prev);
assert!(result.is_err(), "expected a panic");
let file = slot.lock().unwrap().clone();
file
}

let here = file!();

assert_eq!(here, panic_file(|| {
let mut v = ArrayVec::<i32, 8>::new();
v.insert(3, 1);
}));
assert_eq!(here, panic_file(|| {
let mut v = ArrayVec::<i32, 8>::new();
let _ = v.try_insert(3, 1);
}));
assert_eq!(here, panic_file(|| {
let mut v = ArrayVec::<i32, 8>::new();
v.remove(3);
}));
assert_eq!(here, panic_file(|| {
let mut v = ArrayVec::<i32, 8>::new();
v.swap_remove(3);
}));

// The capacity panic of `insert` already reported the caller; keep it that way.
assert_eq!(here, panic_file(|| {
let mut v = ArrayVec::<i32, 1>::new();
v.push(0);
v.insert(0, 1);
}));
}
Loading