diff options
Diffstat (limited to 'src/utils')
| -rw-r--r-- | src/utils/id.rs | 27 | ||||
| -rw-r--r-- | src/utils/mod.rs | 1 |
2 files changed, 28 insertions, 0 deletions
diff --git a/src/utils/id.rs b/src/utils/id.rs new file mode 100644 index 00000000..3f8a2a7d --- /dev/null +++ b/src/utils/id.rs @@ -0,0 +1,27 @@ +use std::sync::atomic::{AtomicU32, Ordering}; + +/// Counter that returns unique IDs. +/// +/// Under the hood it uses a `u32` that will eventually wrap around. When incrementing it once a +/// second, it will wrap around after about 136 years. +pub struct IdCounter { + value: AtomicU32, +} + +impl IdCounter { + pub const fn new() -> Self { + Self { + value: AtomicU32::new(0), + } + } + + pub fn next(&self) -> u32 { + self.value.fetch_add(1, Ordering::SeqCst) + } +} + +impl Default for IdCounter { + fn default() -> Self { + Self::new() + } +} diff --git a/src/utils/mod.rs b/src/utils/mod.rs index e9e56bcf..4902bd3a 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -14,6 +14,7 @@ use smithay::output::Output; use smithay::reexports::rustix::time::{clock_gettime, ClockId}; use smithay::utils::{Logical, Point, Rectangle, Size}; +pub mod id; pub mod spawning; pub mod watcher; |
