use std::{
sync::{
atomic::{AtomicUsize, Ordering},
Arc,
},
time::SystemTime,
};
use crate::unit::Unit;
pub mod key;
#[doc(inline)]
pub use key::Key;
mod utils;
#[cfg(feature = "progress-log")]
mod log;
pub use utils::{Discard, DoOrDiscard, Either, ThroughputOnDrop};
#[cfg(feature = "progress-log")]
pub use self::log::Log;
pub type Id = [u8; 4];
pub const UNKNOWN: Id = *b"\0\0\0\0";
pub type Step = usize;
pub type StepShared = Arc<AtomicUsize>;
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Debug, Hash)]
pub enum State {
Blocked(&'static str, Option<SystemTime>),
Halted(&'static str, Option<SystemTime>),
Running,
}
impl Default for State {
fn default() -> Self {
State::Running
}
}
#[derive(Clone, Default, Debug)]
pub struct Value {
pub step: StepShared,
pub done_at: Option<Step>,
pub unit: Option<Unit>,
pub state: State,
}
impl std::hash::Hash for Value {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
let Self {
step,
done_at,
unit,
state: our_state,
} = self;
done_at.hash(state);
unit.hash(state);
our_state.hash(state);
step.load(Ordering::Relaxed).hash(state);
}
}
impl Value {
pub fn fraction(&self) -> Option<f32> {
self.done_at
.map(|done_at| self.step.load(Ordering::SeqCst) as f32 / done_at as f32)
}
}
#[derive(Clone, Default, Debug, Hash)]
pub struct Task {
pub name: String,
pub id: Id,
pub progress: Option<Value>,
}