Skip to content
Open
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
60 changes: 43 additions & 17 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,17 @@ const fn is_zst<T>() -> bool {
/// and thus cannot be used yet.
fn slice_range<R>(range: R, bounds: core::ops::RangeTo<usize>) -> core::ops::Range<usize>
where R: core::ops::RangeBounds<usize> {
#[cold]
#[inline(never)]
#[track_caller]
fn assert_failed(start: usize, end: usize, len: usize) -> ! {
if start > end {
panic!("slice index starts at {start} but ends at {end}");
} else {
panic!("range end index {end} out of range for slice of length {len}");
}
}

let len = bounds.end;

let start = match range.start_bound() {
Expand All @@ -187,11 +198,8 @@ where R: core::ops::RangeBounds<usize> {
core::ops::Bound::Unbounded => len
};

if start > end {
panic!("slice index starts at {start} but ends at {end}");
}
if end > len {
panic!("range end index {end} out of range for slice of length {len}");
if start > end || end > len {
assert_failed(start, end, len);
}

core::ops::Range {
Expand Down Expand Up @@ -1368,11 +1376,17 @@ impl<T, const N: usize> SmallVec<T, N> {

#[inline]
pub fn swap_remove(&mut self, index: usize) -> T {
#[cold]
#[inline(never)]
#[track_caller]
fn assert_failed(index: usize, len: usize) -> ! {
panic!("swap_remove index (is {index}) should be < len (is {len})");
}

let len = self.len();
assert!(
index < len,
"swap_remove index (is {index}) should be < len (is {len})"
);
if index >= len {
assert_failed(index, len);
}
// This can't overflow since `len > index >= 0`
let new_len = len - 1;
unsafe {
Expand Down Expand Up @@ -1403,11 +1417,17 @@ impl<T, const N: usize> SmallVec<T, N> {

#[inline]
pub fn remove(&mut self, index: usize) -> T {
#[cold]
#[inline(never)]
#[track_caller]
fn assert_failed(index: usize, len: usize) -> ! {
panic!("removal index (is {index}) should be < len (is {len})");
}

let len = self.len();
assert!(
index < len,
"removal index (is {index}) should be < len (is {len})"
);
if index >= len {
assert_failed(index, len);
}
let new_len = len - 1;
unsafe {
// SAFETY: new_len < len
Expand All @@ -1429,11 +1449,17 @@ impl<T, const N: usize> SmallVec<T, N> {
#[inline]
#[must_use]
pub fn insert_mut(&mut self, index: usize, value: T) -> &mut T {
#[cold]
#[inline(never)]
#[track_caller]
fn assert_failed(index: usize, len: usize) -> ! {
panic!("insertion index (is {index}) should be <= len (is {len})");
}

let len = self.len();
assert!(
index <= len,
"insertion index (is {index}) should be <= len (is {len})"
);
if index > len {
assert_failed(index, len);
}
self.reserve(1);

// SAFETY: `index <= len <= capacity`,
Expand Down
Loading