Skip to content

Commit

Permalink
don't duplicate slice panic_bounds_check
Browse files Browse the repository at this point in the history
  • Loading branch information
ibraheemdev committed Oct 14, 2021
1 parent c517a0d commit cf12732
Showing 1 changed file with 4 additions and 10 deletions.
14 changes: 4 additions & 10 deletions library/core/src/slice/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -560,8 +560,8 @@ impl<T> [T] {
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn swap(&mut self, a: usize, b: usize) {
assert_in_bounds(self.len(), a);
assert_in_bounds(self.len(), b);
let _ = &self[a];
let _ = &self[b];

// SAFETY: we just checked that both `a` and `b` are in bounds
unsafe { self.swap_unchecked(a, b) }
Expand Down Expand Up @@ -598,8 +598,8 @@ impl<T> [T] {
pub unsafe fn swap_unchecked(&mut self, a: usize, b: usize) {
#[cfg(debug_assertions)]
{
assert_in_bounds(self.len(), a);
assert_in_bounds(self.len(), b);
let _ = &self[a];
let _ = &self[b];
}

let ptr = self.as_mut_ptr();
Expand Down Expand Up @@ -3502,12 +3502,6 @@ impl<T> [T] {
}
}

fn assert_in_bounds(len: usize, idx: usize) {
if idx >= len {
panic!("index out of bounds: the len is {} but the index is {}", len, idx);
}
}

trait CloneFromSpec<T> {
fn spec_clone_from(&mut self, src: &[T]);
}
Expand Down

0 comments on commit cf12732

Please sign in to comment.