Skip to content

Commit

Permalink
Add #[track_caller] attributes to functions that may panic
Browse files Browse the repository at this point in the history
  • Loading branch information
savannstm committed Jan 13, 2025
1 parent 31c9862 commit 7b64edc
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ impl<K, V, S> IndexMap<K, V, S> {
///
/// ***Panics*** if the starting point is greater than the end point or if
/// the end point is greater than the length of the map.
#[track_caller]
pub fn drain<R>(&mut self, range: R) -> Drain<'_, K, V>
where
R: RangeBounds<usize>,
Expand All @@ -313,6 +314,7 @@ impl<K, V, S> IndexMap<K, V, S> {
/// the elements `[0, at)` with its previous capacity unchanged.
///
/// ***Panics*** if `at > len`.
#[track_caller]
pub fn split_off(&mut self, at: usize) -> Self
where
S: Clone,
Expand Down Expand Up @@ -493,6 +495,7 @@ where
/// assert_eq!(map.get_index_of(&'+'), Some(27));
/// assert_eq!(map.len(), 28);
/// ```
#[track_caller]
pub fn insert_before(&mut self, mut index: usize, key: K, value: V) -> (usize, Option<V>) {
assert!(index <= self.len(), "index out of bounds");
match self.entry(key) {
Expand Down Expand Up @@ -571,6 +574,7 @@ where
/// // This is an invalid index for moving an existing key!
/// map.shift_insert(map.len(), 'a', ());
/// ```
#[track_caller]
pub fn shift_insert(&mut self, index: usize, key: K, value: V) -> Option<V> {
let len = self.len();
match self.entry(key) {
Expand Down Expand Up @@ -627,6 +631,7 @@ where
/// assert!(map.into_iter().eq([(0, '_'), (1, 'A'), (5, 'E'), (3, 'C'), (2, 'B'), (4, 'D')]));
/// assert_eq!(removed, &[(2, 'b'), (3, 'c')]);
/// ```
#[track_caller]
pub fn splice<R, I>(&mut self, range: R, replace_with: I) -> Splice<'_, I::IntoIter, K, V, S>
where
R: RangeBounds<usize>,
Expand Down Expand Up @@ -1278,6 +1283,7 @@ impl<K, V, S> IndexMap<K, V, S> {
/// ***Panics*** if `from` or `to` are out of bounds.
///
/// Computes in **O(n)** time (average).
#[track_caller]
pub fn move_index(&mut self, from: usize, to: usize) {
self.core.move_index(from, to)
}
Expand All @@ -1287,6 +1293,7 @@ impl<K, V, S> IndexMap<K, V, S> {
/// ***Panics*** if `a` or `b` are out of bounds.
///
/// Computes in **O(1)** time (average).
#[track_caller]
pub fn swap_indices(&mut self, a: usize, b: usize) {
self.core.swap_indices(a, b)
}
Expand Down
7 changes: 7 additions & 0 deletions src/map/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ impl<K, V> IndexMapCore<K, V> {
}
}

#[track_caller]
pub(crate) fn drain<R>(&mut self, range: R) -> vec::Drain<'_, Bucket<K, V>>
where
R: RangeBounds<usize>,
Expand All @@ -205,6 +206,7 @@ impl<K, V> IndexMapCore<K, V> {
self.entries.par_drain(range)
}

#[track_caller]
pub(crate) fn split_off(&mut self, at: usize) -> Self {
assert!(at <= self.entries.len());
self.erase_indices(at, self.entries.len());
Expand All @@ -215,6 +217,7 @@ impl<K, V> IndexMapCore<K, V> {
Self { indices, entries }
}

#[track_caller]
pub(crate) fn split_splice<R>(&mut self, range: R) -> (Self, vec::IntoIter<Bucket<K, V>>)
where
R: RangeBounds<usize>,
Expand Down Expand Up @@ -403,11 +406,13 @@ impl<K, V> IndexMapCore<K, V> {
}

#[inline]
#[track_caller]
pub(super) fn move_index(&mut self, from: usize, to: usize) {
self.borrow_mut().move_index(from, to);
}

#[inline]
#[track_caller]
pub(crate) fn swap_indices(&mut self, a: usize, b: usize) {
self.borrow_mut().swap_indices(a, b);
}
Expand Down Expand Up @@ -670,6 +675,7 @@ impl<'a, K, V> RefMut<'a, K, V> {
}
}

#[track_caller]
fn move_index(&mut self, from: usize, to: usize) {
let from_hash = self.entries[from].hash;
let _ = self.entries[to]; // explicit bounds check
Expand All @@ -691,6 +697,7 @@ impl<'a, K, V> RefMut<'a, K, V> {
}
}

#[track_caller]
fn swap_indices(&mut self, a: usize, b: usize) {
// If they're equal and in-bounds, there's nothing to do.
if a == b && a < self.entries.len() {
Expand Down
2 changes: 2 additions & 0 deletions src/map/core/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ impl<'a, K, V> OccupiedEntry<'a, K, V> {
/// ***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 Down Expand Up @@ -532,6 +533,7 @@ impl<'a, K, V> IndexedEntry<'a, K, V> {
/// ***Panics*** if `to` is out of bounds.
///
/// Computes in **O(n)** time (average).
#[track_caller]
pub fn move_index(mut self, to: usize) {
self.map.move_index(self.index, to);
}
Expand Down
1 change: 1 addition & 0 deletions src/map/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -667,6 +667,7 @@ where
K: Hash + Eq,
S: BuildHasher,
{
#[track_caller]
pub(super) fn new<R>(map: &'a mut IndexMap<K, V, S>, range: R, replace_with: I) -> Self
where
R: RangeBounds<usize>,
Expand Down
7 changes: 7 additions & 0 deletions src/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ impl<T, S> IndexSet<T, S> {
///
/// ***Panics*** if the starting point is greater than the end point or if
/// the end point is greater than the length of the set.
#[track_caller]
pub fn drain<R>(&mut self, range: R) -> Drain<'_, T>
where
R: RangeBounds<usize>,
Expand All @@ -264,6 +265,7 @@ impl<T, S> IndexSet<T, S> {
/// the elements `[0, at)` with its previous capacity unchanged.
///
/// ***Panics*** if `at > len`.
#[track_caller]
pub fn split_off(&mut self, at: usize) -> Self
where
S: Clone,
Expand Down Expand Up @@ -426,6 +428,7 @@ where
/// assert_eq!(set.get_index_of(&'+'), Some(27));
/// assert_eq!(set.len(), 28);
/// ```
#[track_caller]
pub fn insert_before(&mut self, index: usize, value: T) -> (usize, bool) {
let (index, existing) = self.map.insert_before(index, value, ());
(index, existing.is_none())
Expand Down Expand Up @@ -483,6 +486,7 @@ where
/// // This is an invalid index for moving an existing value!
/// set.shift_insert(set.len(), 'a');
/// ```
#[track_caller]
pub fn shift_insert(&mut self, index: usize, value: T) -> bool {
self.map.shift_insert(index, value, ()).is_none()
}
Expand Down Expand Up @@ -584,6 +588,7 @@ where
/// assert!(set.into_iter().eq([0, 1, 5, 3, 2, 4]));
/// assert_eq!(removed, &[2, 3]);
/// ```
#[track_caller]
pub fn splice<R, I>(&mut self, range: R, replace_with: I) -> Splice<'_, I::IntoIter, T, S>
where
R: RangeBounds<usize>,
Expand Down Expand Up @@ -1050,6 +1055,7 @@ impl<T, S> IndexSet<T, S> {
/// ***Panics*** if `from` or `to` are out of bounds.
///
/// Computes in **O(n)** time (average).
#[track_caller]
pub fn move_index(&mut self, from: usize, to: usize) {
self.map.move_index(from, to)
}
Expand All @@ -1059,6 +1065,7 @@ impl<T, S> IndexSet<T, S> {
/// ***Panics*** if `a` or `b` are out of bounds.
///
/// Computes in **O(1)** time (average).
#[track_caller]
pub fn swap_indices(&mut self, a: usize, b: usize) {
self.map.swap_indices(a, b)
}
Expand Down
1 change: 1 addition & 0 deletions src/set/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,7 @@ where
T: Hash + Eq,
S: BuildHasher,
{
#[track_caller]
pub(super) fn new<R>(set: &'a mut IndexSet<T, S>, range: R, replace_with: I) -> Self
where
R: RangeBounds<usize>,
Expand Down
1 change: 1 addition & 0 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pub(crate) fn third<A, B, C>(t: (A, B, C)) -> C {
t.2
}

#[track_caller]
pub(crate) fn simplify_range<R>(range: R, len: usize) -> Range<usize>
where
R: RangeBounds<usize>,
Expand Down

0 comments on commit 7b64edc

Please sign in to comment.