diff --git a/src/map.rs b/src/map.rs index 79a45527..cf0c70a9 100644 --- a/src/map.rs +++ b/src/map.rs @@ -1474,14 +1474,14 @@ impl Index for IndexMap { /// /// ***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() + ); + } } } @@ -1521,11 +1521,11 @@ impl IndexMut for IndexMap { 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}"); + } } } diff --git a/src/map/core/entry.rs b/src/map/core/entry.rs index 6ab29ca5..39a20d5f 100644 --- a/src/map/core/entry.rs +++ b/src/map/core/entry.rs @@ -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); @@ -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); @@ -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); } diff --git a/src/map/core/raw_entry_v1.rs b/src/map/core/raw_entry_v1.rs index 757a3cee..e76793b7 100644 --- a/src/map/core/raw_entry_v1.rs +++ b/src/map/core/raw_entry_v1.rs @@ -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); @@ -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); @@ -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, @@ -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, diff --git a/src/map/slice.rs b/src/map/slice.rs index 035744ef..ab1cbb1f 100644 --- a/src/map/slice.rs +++ b/src/map/slice.rs @@ -125,6 +125,7 @@ impl Slice { /// 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)) @@ -133,6 +134,7 @@ impl Slice { /// 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)) diff --git a/src/set.rs b/src/set.rs index 1be248eb..d932735c 100644 --- a/src/set.rs +++ b/src/set.rs @@ -1106,12 +1106,14 @@ impl Index for IndexSet { /// /// ***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() ); - }) + } } } diff --git a/src/set/slice.rs b/src/set/slice.rs index faa9041a..40a8b6c3 100644 --- a/src/set/slice.rs +++ b/src/set/slice.rs @@ -85,6 +85,7 @@ impl Slice { /// 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))