#![deny(unsafe_code)]
#![warn(
clippy::filetype_is_file,
clippy::integer_division,
clippy::needless_borrow,
clippy::nursery,
clippy::pedantic,
clippy::perf,
clippy::suboptimal_flops,
clippy::unneeded_field_pattern,
macro_use_extern_crate,
missing_copy_implementations,
missing_debug_implementations,
missing_docs,
non_ascii_idents,
trivial_casts,
trivial_numeric_casts,
unreachable_pub,
unused_crate_dependencies,
unused_extern_crates,
unused_import_braces,
)]
#![allow(
clippy::module_name_repetitions,
clippy::redundant_pub_crate
)]
#[macro_use] mod macros;
mod hash;
mod nice_elapsed;
mod nice_int;
pub mod traits;
pub use hash::NoHash;
pub use nice_elapsed::NiceElapsed;
pub use nice_int::{
nice_u8::NiceU8,
nice_u16::NiceU16,
nice_u32::NiceU32,
nice_u64::NiceU64,
nice_float::{
FloatKind,
NiceFloat,
},
nice_percent::NicePercent,
};
#[doc(hidden)]
pub use nice_int::NiceWrapper;
use num_traits::cast::AsPrimitive;
static DOUBLE: [[u8; 2]; 100] = [
[48, 48], [48, 49], [48, 50], [48, 51], [48, 52], [48, 53], [48, 54], [48, 55], [48, 56], [48, 57],
[49, 48], [49, 49], [49, 50], [49, 51], [49, 52], [49, 53], [49, 54], [49, 55], [49, 56], [49, 57],
[50, 48], [50, 49], [50, 50], [50, 51], [50, 52], [50, 53], [50, 54], [50, 55], [50, 56], [50, 57],
[51, 48], [51, 49], [51, 50], [51, 51], [51, 52], [51, 53], [51, 54], [51, 55], [51, 56], [51, 57],
[52, 48], [52, 49], [52, 50], [52, 51], [52, 52], [52, 53], [52, 54], [52, 55], [52, 56], [52, 57],
[53, 48], [53, 49], [53, 50], [53, 51], [53, 52], [53, 53], [53, 54], [53, 55], [53, 56], [53, 57],
[54, 48], [54, 49], [54, 50], [54, 51], [54, 52], [54, 53], [54, 54], [54, 55], [54, 56], [54, 57],
[55, 48], [55, 49], [55, 50], [55, 51], [55, 52], [55, 53], [55, 54], [55, 55], [55, 56], [55, 57],
[56, 48], [56, 49], [56, 50], [56, 51], [56, 52], [56, 53], [56, 54], [56, 55], [56, 56], [56, 57],
[57, 48], [57, 49], [57, 50], [57, 51], [57, 52], [57, 53], [57, 54], [57, 55], [57, 56], [57, 57]
];
#[allow(unsafe_code)]
#[inline]
pub(crate) fn double_ptr(idx: usize) -> *const u8 {
debug_assert!(idx < 100, "BUG: Invalid index passed to double_ptr.");
unsafe { DOUBLE.get_unchecked(idx).as_ptr() }
}
pub(crate) fn double(idx: usize) -> [u8; 2] { DOUBLE[idx] }
#[must_use]
pub fn div_mod<T>(e: T, d: T) -> (T, T)
where T: Copy + std::ops::Div<Output=T> + std::ops::Rem<Output=T> { (e / d, e % d) }
#[must_use]
pub fn int_div_float<T>(e: T, d: T) -> Option<f64>
where T: AsPrimitive<f64> {
let d: f64 = d.as_();
if d == 0.0 { None }
else {
Some(e.as_() / d).filter(|x| x.is_finite())
}
}
#[cfg(test)]
mod tests {
use super::*;
use brunch as _;
#[test]
fn t_int_div_float() {
assert_eq!(int_div_float(4_000_000_000_u64, 8_000_000_000_u64), Some(0.5));
assert_eq!(int_div_float(400_000_000_000_u64, 800_000_000_000_u64), Some(0.5));
assert_eq!(int_div_float(400_000_000_000_u64, 0_u64), None);
assert_eq!(int_div_float(4_u8, 8_u8), Some(0.5));
}
}