forge
Explore
Log in

Don't you dare go Hollow

niri

Clone
★ 0

0 watching0 forksRustGPL-3.012.1 MiBf51dd67Ivan Molodetskikh2 years ago981 commits

id.rs27 lines, 583 bytesHistoryRawDownload
1use std::sync::atomic::{AtomicU32, Ordering};
2
3/// Counter that returns unique IDs.
4///
5/// Under the hood it uses a `u32` that will eventually wrap around. When incrementing it once a
6/// second, it will wrap around after about 136 years.
7pub struct IdCounter {
8 value: AtomicU32,
9}
10
11impl IdCounter {
12 pub const fn new() -> Self {
13 Self {
14 value: AtomicU32::new(0),
15 }
16 }
17
18 pub fn next(&self) -> u32 {
19 self.value.fetch_add(1, Ordering::SeqCst)
20 }
21}
22
23impl Default for IdCounter {
24 fn default() -> Self {
25 Self::new()
26 }
27}