use std::sync::atomic::{AtomicU64, Ordering};
static TASK_ID_SEQ: AtomicU64 = AtomicU64::new(1);
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct TaskId(u64);
impl TaskId {
#[inline]
pub(crate) fn next() -> Self {
TaskId(TASK_ID_SEQ.fetch_add(1, Ordering::Relaxed))
}
#[inline]
pub fn get(self) -> u64 {
self.0
}
}
impl std::fmt::Display for TaskId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "#{}", self.0)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn ids_are_unique_and_monotonic() {
let a = TaskId::next();
let b = TaskId::next();
assert!(b.get() > a.get(), "ids must increase: {a} then {b}");
assert_ne!(a, b);
}
#[test]
fn never_mints_zero() {
assert!(TaskId::next().get() >= 1);
}
}