1use core::fmt;
2use core::sync::atomic::{AtomicUsize, Ordering};
3
4#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord)]
5pub struct ProcessId(usize);
6
7impl ProcessId {
8 pub fn new() -> Self {
9 static NEXT_PID: AtomicUsize = AtomicUsize::new(1);
10 Self(NEXT_PID.fetch_add(1, Ordering::Relaxed))
11 }
12
13 pub fn as_usize(self) -> usize {
14 self.0
15 }
16
17 pub const fn from_usize(pid: usize) -> Self {
18 Self(pid)
19 }
20
21 pub fn is_init(self) -> bool {
22 self.0 == 1
23 }
24}
25
26impl fmt::Display for ProcessId {
27 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
28 write!(f, "{}", self.0)
29 }
30}
31
32#[cfg(test)]
33mod tests {
34 use super::*;
35
36 #[test]
37 fn pid_from_usize_preserves_value() {
38 assert_eq!(ProcessId::from_usize(42).as_usize(), 42);
39 }
40}