1use core::fmt;
2
3#[derive(Copy, Clone, Eq, PartialEq, Hash)]
19pub struct TaskId(u64);
20
21impl TaskId {
22 pub(crate) fn next() -> Self {
23 use portable_atomic::{AtomicU64, Ordering::Relaxed};
25
26 static NEXT_ID: AtomicU64 = AtomicU64::new(1);
28 let id = NEXT_ID.fetch_add(1, Relaxed);
29
30 debug_assert!(id > 0, "64-bit task ID counter should not overflow!");
31 Self(id)
32 }
33
34 #[must_use]
35 #[inline]
36 pub(crate) const fn stub() -> Self {
37 Self(0)
38 }
39
40 #[allow(dead_code)]
41 #[must_use]
42 #[inline]
43 pub(crate) fn is_stub(self) -> bool {
44 self.0 == 0
45 }
46
47 #[inline]
48 #[allow(dead_code)] pub(crate) fn as_u64(self) -> u64 {
50 self.0
51 }
52}
53
54impl fmt::UpperHex for TaskId {
55 #[inline]
56 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
57 f.write_str("TaskId(")?;
58 fmt::UpperHex::fmt(&self.0, f)?;
59 f.write_str(")")
60 }
61}
62
63impl fmt::LowerHex for TaskId {
64 #[inline]
65 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
66 f.write_str("TaskId(")?;
67 fmt::LowerHex::fmt(&self.0, f)?;
68 f.write_str(")")
69 }
70}
71
72impl fmt::Debug for TaskId {
73 #[inline]
74 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
75 f.write_str("TaskId(")?;
76 fmt::Debug::fmt(&self.0, f)?;
77 f.write_str(")")
78 }
79}
80
81impl fmt::Display for TaskId {
82 #[inline]
83 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
84 fmt::Display::fmt(&self.0, f)
85 }
86}
87
88impl PartialEq<&'_ TaskId> for TaskId {
91 #[inline]
92 fn eq(&self, other: &&Self) -> bool {
93 self.0 == other.0
94 }
95}
96
97impl PartialEq<TaskId> for &'_ TaskId {
98 #[inline]
99 fn eq(&self, other: &TaskId) -> bool {
100 self.0 == other.0
101 }
102}