-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathgenerational_ids.rs
54 lines (46 loc) · 1.29 KB
/
generational_ids.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// Copyright 2025-Present Datadog, Inc. https://www.datadoghq.com/
// SPDX-License-Identifier: Apache-2.0
use std::sync::atomic::AtomicU64;
/// Opaque identifier for the profiler generation
#[derive(Clone, Copy, PartialEq, Eq)]
#[repr(C)]
pub struct Generation {
id: u64,
}
impl Generation {
const IMMORTAL: Self = Self { id: u64::MAX };
/// The only way to create a generation. Guaranteed to give a new value each time.
pub fn new() -> Self {
static COUNTER: AtomicU64 = AtomicU64::new(0);
Self {
id: COUNTER.fetch_add(1, std::sync::atomic::Ordering::SeqCst),
}
}
}
impl Default for Generation {
fn default() -> Self {
Self::new()
}
}
#[repr(C)]
pub struct GenerationalId<T: Copy> {
generation: Generation,
id: T,
}
impl<T: Copy> GenerationalId<T> {
pub fn get(&self, expected_generation: Generation) -> anyhow::Result<T> {
anyhow::ensure!(
self.generation == expected_generation || self.generation == Generation::IMMORTAL
);
Ok(self.id)
}
pub const fn new(id: T, generation: Generation) -> Self {
Self { id, generation }
}
pub const fn new_immortal(id: T) -> Self {
Self {
id,
generation: Generation::IMMORTAL,
}
}
}