use std::mem;
enum Void {}
#[inline]
pub unsafe fn boom(msg: &str) -> ! {
debug_assert!(false, "BOOM! {}", msg);
let v: &Void = mem::transmute(0usize);
match *v {}
}
pub trait OptionExt<T> {
unsafe fn boom_some(self) -> T;
unsafe fn boom_none(self);
unsafe fn boom_take(&mut self) -> T;
}
pub trait ResultExt<T, E> {
unsafe fn boom_ok(self) -> T;
unsafe fn boom_err(self) -> E;
}
pub trait SliceExt<T> {
unsafe fn boom_get(&self, index: usize) -> &T;
unsafe fn boom_get_mut(&mut self, index: usize) -> &mut T;
}
impl<T> OptionExt<T> for Option<T> {
#[inline]
unsafe fn boom_some(self) -> T {
match self {
Some(x) => x,
None => boom("Expected Some got None"),
}
}
#[inline]
unsafe fn boom_none(self) {
match self {
Some(_) => boom("Expected None, got Some"),
None => (),
}
}
#[inline]
unsafe fn boom_take(&mut self) -> T {
match self.take() {
Some(x) => x,
None => boom("Expected None, got Some"),
}
}
}
impl<T, E> ResultExt<T, E> for Result<T, E> {
#[inline]
unsafe fn boom_ok(self) -> T {
match self {
Ok(x) => x,
Err(_) => boom("Expected Ok, got Err"),
}
}
#[inline]
unsafe fn boom_err(self) -> E {
match self {
Ok(_) => boom("Expected Err, got Ok"),
Err(e) => e,
}
}
}
impl<T> SliceExt<T> for [T] {
#[inline]
unsafe fn boom_get(&self, index: usize) -> &T {
debug_assert!(index < self.len(), "BOOM! Index out of bounds!");
self.get_unchecked(index)
}
#[inline]
unsafe fn boom_get_mut(&mut self, index: usize) -> &mut T {
debug_assert!(index < self.len(), "BOOM! Index out of bounds!");
self.get_unchecked_mut(index)
}
}