Make panic's more specific
This commit is contained in:
parent
64d143326f
commit
7398b39a0b
1 changed files with 13 additions and 10 deletions
|
@ -1,6 +1,7 @@
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use std::marker::PhantomData;
|
use std::marker::PhantomData;
|
||||||
use std::ops::{Index, IndexMut};
|
use std::ops::{Index, IndexMut};
|
||||||
|
use std::slice::GetDisjointMutError::*;
|
||||||
use std::slice::{self, SliceIndex};
|
use std::slice::{self, SliceIndex};
|
||||||
|
|
||||||
use crate::{Idx, IndexVec, IntoSliceIdx};
|
use crate::{Idx, IndexVec, IntoSliceIdx};
|
||||||
|
@ -115,33 +116,35 @@ impl<I: Idx, T> IndexSlice<I, T> {
|
||||||
|
|
||||||
/// Returns mutable references to two distinct elements, `a` and `b`.
|
/// Returns mutable references to two distinct elements, `a` and `b`.
|
||||||
///
|
///
|
||||||
/// Panics if `a == b`.
|
/// Panics if `a == b` or if some of them are out of bounds.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn pick2_mut(&mut self, a: I, b: I) -> (&mut T, &mut T) {
|
pub fn pick2_mut(&mut self, a: I, b: I) -> (&mut T, &mut T) {
|
||||||
let (ai, bi) = (a.index(), b.index());
|
let (ai, bi) = (a.index(), b.index());
|
||||||
assert!(ai != bi);
|
|
||||||
let len = self.raw.len();
|
|
||||||
assert!(ai < len && bi < len);
|
|
||||||
|
|
||||||
match self.raw.get_disjoint_mut([ai, bi]) {
|
match self.raw.get_disjoint_mut([ai, bi]) {
|
||||||
Ok([a, b]) => (a, b),
|
Ok([a, b]) => (a, b),
|
||||||
Err(_) => panic!("Can't get mutable references"),
|
Err(OverlappingIndices) => panic!("Indices {ai:?} and {bi:?} are not disjoint!"),
|
||||||
|
Err(IndexOutOfBounds) => {
|
||||||
|
panic!("Some indices among ({ai:?}, {bi:?}) are out of bounds")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns mutable references to three distinct elements.
|
/// Returns mutable references to three distinct elements.
|
||||||
///
|
///
|
||||||
/// Panics if the elements are not distinct.
|
/// Panics if the elements are not distinct or if some of them are out of bounds.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn pick3_mut(&mut self, a: I, b: I, c: I) -> (&mut T, &mut T, &mut T) {
|
pub fn pick3_mut(&mut self, a: I, b: I, c: I) -> (&mut T, &mut T, &mut T) {
|
||||||
let (ai, bi, ci) = (a.index(), b.index(), c.index());
|
let (ai, bi, ci) = (a.index(), b.index(), c.index());
|
||||||
assert!(ai != bi && bi != ci && ci != ai);
|
|
||||||
let len = self.raw.len();
|
|
||||||
assert!(ai < len && bi < len && ci < len);
|
|
||||||
|
|
||||||
match self.raw.get_disjoint_mut([ai, bi, ci]) {
|
match self.raw.get_disjoint_mut([ai, bi, ci]) {
|
||||||
Ok([a, b, c]) => (a, b, c),
|
Ok([a, b, c]) => (a, b, c),
|
||||||
Err(_) => panic!("Can't get mutable references"),
|
Err(OverlappingIndices) => {
|
||||||
|
panic!("Indices {ai:?}, {bi:?} and {ci:?} are not disjoint!")
|
||||||
|
}
|
||||||
|
Err(IndexOutOfBounds) => {
|
||||||
|
panic!("Some indices among ({ai:?}, {bi:?}, {ci:?}) are out of bounds")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue