const fn str_eq(a: &str, b: &str) -> bool {
let a = a.as_bytes();
let b = b.as_bytes();
if a.len() != b.len() {
return false;
}
let mut i = 0;
while i < a.len() {
if a[i] != b[i] {
return false;
}
i += 1;
}
true
}
pub(crate) const GLOBAL_OVERFLOW_CHECKS: bool = {
match option_env!("BNUM_OVERFLOW_CHECKS") {
Some(v) if str_eq(v, "true") => true,
Some(v) if str_eq(v, "false") => false,
_ => cfg!(debug_assertions), }
};
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
#[repr(u8)]
pub enum OverflowMode {
Wrap = 0,
Panic = 1,
Saturate = 2,
}
impl OverflowMode {
pub const DEFAULT: Self = if GLOBAL_OVERFLOW_CHECKS {
OverflowMode::Panic
} else {
OverflowMode::Wrap
};
#[inline]
pub(crate) const fn to_u8(self) -> u8 {
self as u8
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_str_eq() {
assert!(str_eq("hello", "hello"));
assert!(!str_eq("hello", "world"));
assert!(str_eq("", ""));
assert!(str_eq("a", "a"));
assert!(str_eq("hi there", "hi there"));
assert!(!str_eq("hello", "helloo"));
assert!(!str_eq("", "a"));
}
#[test]
fn test_overflow_mode_to_u8() {
assert_eq!(OverflowMode::Wrap.to_u8(), 0);
assert_eq!(OverflowMode::Panic.to_u8(), 1);
assert_eq!(OverflowMode::Saturate.to_u8(), 2);
}
}