use dactyl::NiceFloat;
use std::num::NonZeroU64;
#[cfg_attr(docsrs, doc(cfg(feature = "progress")))]
#[derive(Debug, Copy, Clone)]
pub struct BeforeAfter {
before: Option<NonZeroU64>,
after: Option<NonZeroU64>,
}
impl From<(u64, u64)> for BeforeAfter {
#[inline]
fn from(src: (u64, u64)) -> Self {
Self {
before: NonZeroU64::new(src.0),
after: NonZeroU64::new(src.1),
}
}
}
impl BeforeAfter {
#[must_use]
#[inline]
pub const fn start(before: u64) -> Self {
Self {
before: NonZeroU64::new(before),
after: None,
}
}
#[inline]
pub const fn stop(&mut self, after: u64) {
self.after = NonZeroU64::new(after);
}
#[must_use]
#[inline]
pub const fn before(&self) -> Option<NonZeroU64> { self.before }
#[must_use]
#[inline]
pub const fn after(&self) -> Option<NonZeroU64> { self.after }
#[must_use]
#[inline]
pub const fn less(&self) -> Option<NonZeroU64> {
if let Some(b) = self.before && let Some(a) = self.after {
NonZeroU64::new(b.get().saturating_sub(a.get()))
}
else { None }
}
#[must_use]
#[inline]
pub const fn less_percent(&self) -> Option<f64> {
if
let Some(b) = self.before &&
let Some(l) = self.less() &&
let Ok(out) = NiceFloat::div_u64(l.get(), b.get())
{
Some(out)
}
else { None }
}
#[must_use]
#[inline]
pub const fn more(&self) -> Option<NonZeroU64> {
if let Some(b) = self.before && let Some(a) = self.after {
NonZeroU64::new(a.get().saturating_sub(b.get()))
}
else { None }
}
#[must_use]
#[inline]
pub const fn more_percent(&self) -> Option<f64> {
if
let Some(b) = self.before &&
let Some(m) = self.more() &&
let Ok(out) = NiceFloat::div_u64(m.get(), b.get())
{
Some(out)
}
else { None }
}
}