Skip to main content

radiate_utils/
id.rs

1#[macro_export]
2macro_rules! sentry_id {
3    ($name:ident) => {
4        #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
5        #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
6        #[repr(transparent)]
7        pub struct $name(u64);
8
9        impl $name {
10            pub const EMPTY: Self = $name(0);
11
12            pub fn new() -> Self {
13                static COUNTER: ::std::sync::atomic::AtomicU64 =
14                    ::std::sync::atomic::AtomicU64::new(1);
15
16                $name(COUNTER.fetch_add(1, ::std::sync::atomic::Ordering::Relaxed))
17            }
18
19            pub const fn is_empty(&self) -> bool {
20                self.0 == 0
21            }
22
23            pub const fn get(&self) -> u64 {
24                self.0
25            }
26
27            pub fn next(&self) -> Self {
28                Self::new()
29            }
30        }
31
32        #[allow(clippy::from_over_into)]
33        impl ::std::convert::Into<u64> for $name {
34            fn into(self) -> u64 {
35                self.0
36            }
37        }
38
39        impl ::std::convert::AsRef<u64> for $name {
40            fn as_ref(&self) -> &u64 {
41                &self.0
42            }
43        }
44
45        impl ::std::default::Default for $name {
46            fn default() -> Self {
47                Self::EMPTY
48            }
49        }
50
51        impl ::std::fmt::Display for $name {
52            fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
53                write!(f, "{}", self.0)
54            }
55        }
56    };
57}