forge
Explore
Log in

Don't you dare go Hollow

niri

Clone
★ 0

0 watching0 forksRustGPL-3.012.1 MiB9d8f4f8Ivan Molodetskikh1 month ago2997 commits

id.rs28 lines, 577 bytesHistoryRawDownload
1use std::sync::atomic::Ordering;
2
3use portable_atomic::AtomicU64;
4
5/// Counter that returns unique IDs.
6pub struct IdCounter {
7 value: AtomicU64,
8}
9
10impl IdCounter {
11 pub const fn new() -> Self {
12 Self {
13 // Start from 1 to reduce the possibility that some other code that uses these IDs will
14 // get confused.
15 value: AtomicU64::new(1),
16 }
17 }
18
19 pub fn next(&self) -> u64 {
20 self.value.fetch_add(1, Ordering::Relaxed)
21 }
22}
23
24impl Default for IdCounter {
25 fn default() -> Self {
26 Self::new()
27 }
28}