#![allow(clippy::needless_doctest_main)] #![forbid(unsafe_code)]
#[cfg(not(debug_assertions))]
compile_error!("repeat after me: do not deploy anything with this awful library");
use std::hint::black_box;
pub fn is_odd(number: i8) -> bool {
definitely_not_transmute(number)
}
enum Sneaky<S, T> {
#[allow(dead_code)]
From(Option<Box<S>>),
To(Option<Box<T>>),
}
#[inline(never)]
fn definitely_not_transmute<S, T>(input: S) -> T {
let mut sneaky: Sneaky<S, T> = Sneaky::To(None);
let outer = &mut sneaky;
let inner = match outer {
Sneaky::To(something) => something,
Sneaky::From(_) => unreachable!(),
};
let inner = expand_mut(inner);
*outer = Sneaky::From(Some(Box::new(input)));
black_box(outer);
*inner.take().unwrap()
}
fn weird<'a, 'b, T>(_witness: &'b &'a (), borrow: &'a mut T) -> &'b mut T {
borrow
}
#[allow(clippy::redundant_static_lifetimes)] const FOREVER: &'static &'static () = &&();
fn expand_mut<'a, 'b, T>(borrow: &'a mut T) -> &'b mut T {
let converted: fn(&'b &'static (), &'a mut T) -> &'b mut T = weird;
converted(FOREVER, borrow)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn positive_numbers() {
assert_eq!(is_odd(1), true);
assert_eq!(is_odd(2), false);
assert_eq!(is_odd(3), true);
assert_eq!(is_odd(42), false);
assert_eq!(is_odd(69), true);
}
#[test]
fn negative_numbers() {
assert_eq!(is_odd(-1), true);
assert_eq!(is_odd(-2), false);
assert_eq!(is_odd(-3), true);
assert_eq!(is_odd(-42), false);
assert_eq!(is_odd(-69), true);
}
#[test]
fn zero() {
assert_eq!(is_odd(0), false);
}
#[test]
fn extremes() {
assert_eq!(is_odd(127), true);
assert_eq!(is_odd(-128), false);
}
}