forge
Explore
Log in

Don't you dare go Hollow

niri

Clone
★ 0

0 watching0 forksRustGPL-3.012.1 MiB055a94dIvan Molodetskikh1 year ago2247 commits

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