Skip to content
Merged
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
26 changes: 13 additions & 13 deletions src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1474,14 +1474,14 @@ impl<K, V, S> Index<usize> for IndexMap<K, V, S> {
///
/// ***Panics*** if `index` is out of bounds.
fn index(&self, index: usize) -> &V {
self.get_index(index)
.unwrap_or_else(|| {
panic!(
"index out of bounds: the len is {len} but the index is {index}",
len = self.len()
);
})
.1
if let Some((_, value)) = self.get_index(index) {
value
} else {
panic!(
"index out of bounds: the len is {len} but the index is {index}",
len = self.len()
);
}
}
}

Expand Down Expand Up @@ -1521,11 +1521,11 @@ impl<K, V, S> IndexMut<usize> for IndexMap<K, V, S> {
fn index_mut(&mut self, index: usize) -> &mut V {
let len: usize = self.len();

self.get_index_mut(index)
.unwrap_or_else(|| {
panic!("index out of bounds: the len is {len} but the index is {index}");
})
.1
if let Some((_, value)) = self.get_index_mut(index) {
value
} else {
panic!("index out of bounds: the len is {len} but the index is {index}");
}
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/map/core/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ impl<'a, K, V> OccupiedEntry<'a, K, V> {
/// ***Panics*** if the `other` index is out of bounds.
///
/// Computes in **O(1)** time (average).
#[track_caller]
pub fn swap_indices(self, other: usize) {
let index = self.index();
self.into_ref_mut().swap_indices(index, other);
Expand Down Expand Up @@ -407,6 +408,7 @@ impl<'a, K, V> VacantEntry<'a, K, V> {
/// ***Panics*** if `index` is out of bounds.
///
/// Computes in **O(n)** time (average).
#[track_caller]
pub fn shift_insert(mut self, index: usize, value: V) -> &'a mut V {
self.map
.shift_insert_unique(index, self.hash, self.key, value);
Expand Down Expand Up @@ -546,6 +548,7 @@ impl<'a, K, V> IndexedEntry<'a, K, V> {
/// ***Panics*** if the `other` index is out of bounds.
///
/// Computes in **O(1)** time (average).
#[track_caller]
pub fn swap_indices(mut self, other: usize) {
self.map.swap_indices(self.index, other);
}
Expand Down
4 changes: 4 additions & 0 deletions src/map/core/raw_entry_v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,7 @@ impl<'a, K, V, S> RawOccupiedEntryMut<'a, K, V, S> {
/// ***Panics*** if `to` is out of bounds.
///
/// Computes in **O(n)** time (average).
#[track_caller]
pub fn move_index(self, to: usize) {
let index = self.index();
self.into_ref_mut().move_index(index, to);
Expand All @@ -579,6 +580,7 @@ impl<'a, K, V, S> RawOccupiedEntryMut<'a, K, V, S> {
/// ***Panics*** if the `other` index is out of bounds.
///
/// Computes in **O(1)** time (average).
#[track_caller]
pub fn swap_indices(self, other: usize) {
let index = self.index();
self.into_ref_mut().swap_indices(index, other);
Expand Down Expand Up @@ -629,6 +631,7 @@ impl<'a, K, V, S> RawVacantEntryMut<'a, K, V, S> {
/// ***Panics*** if `index` is out of bounds.
///
/// Computes in **O(n)** time (average).
#[track_caller]
pub fn shift_insert(self, index: usize, key: K, value: V) -> (&'a mut K, &'a mut V)
where
K: Hash,
Expand All @@ -645,6 +648,7 @@ impl<'a, K, V, S> RawVacantEntryMut<'a, K, V, S> {
/// ***Panics*** if `index` is out of bounds.
///
/// Computes in **O(n)** time (average).
#[track_caller]
pub fn shift_insert_hashed_nocheck(
mut self,
index: usize,
Expand Down
2 changes: 2 additions & 0 deletions src/map/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ impl<K, V> Slice<K, V> {
/// Divides one slice into two at an index.
///
/// ***Panics*** if `index > len`.
#[track_caller]
pub fn split_at(&self, index: usize) -> (&Self, &Self) {
let (first, second) = self.entries.split_at(index);
(Self::from_slice(first), Self::from_slice(second))
Expand All @@ -133,6 +134,7 @@ impl<K, V> Slice<K, V> {
/// Divides one mutable slice into two at an index.
///
/// ***Panics*** if `index > len`.
#[track_caller]
pub fn split_at_mut(&mut self, index: usize) -> (&mut Self, &mut Self) {
let (first, second) = self.entries.split_at_mut(index);
(Self::from_mut_slice(first), Self::from_mut_slice(second))
Expand Down
6 changes: 4 additions & 2 deletions src/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1106,12 +1106,14 @@ impl<T, S> Index<usize> for IndexSet<T, S> {
///
/// ***Panics*** if `index` is out of bounds.
fn index(&self, index: usize) -> &T {
self.get_index(index).unwrap_or_else(|| {
if let Some(value) = self.get_index(index) {
value
} else {
panic!(
"index out of bounds: the len is {len} but the index is {index}",
len = self.len()
);
})
}
}
}

Expand Down
1 change: 1 addition & 0 deletions src/set/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ impl<T> Slice<T> {
/// Divides one slice into two at an index.
///
/// ***Panics*** if `index > len`.
#[track_caller]
pub fn split_at(&self, index: usize) -> (&Self, &Self) {
let (first, second) = self.entries.split_at(index);
(Self::from_slice(first), Self::from_slice(second))
Expand Down