use core::fmt;
use core::sync::atomic::{AtomicUsize, Ordering};
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord)]
pub struct ProcessId(usize);
impl ProcessId {
pub fn new() -> Self {
static NEXT_PID: AtomicUsize = AtomicUsize::new(1);
Self(NEXT_PID.fetch_add(1, Ordering::Relaxed))
}
pub fn as_usize(self) -> usize {
self.0
}
pub const fn from_usize(pid: usize) -> Self {
Self(pid)
}
pub fn is_init(self) -> bool {
self.0 == 1
}
}
impl fmt::Display for ProcessId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn pid_from_usize_preserves_value() {
assert_eq!(ProcessId::from_usize(42).as_usize(), 42);
}
}