rune/shared/
gen.rs

1use core::cell::Cell;
2use core::num::NonZeroU32;
3
4use crate::parse::NonZeroId;
5
6#[derive(Debug)]
7pub(crate) struct Gen {
8    id: Cell<u32>,
9}
10
11impl Gen {
12    /// Construct a new shared generator.
13    pub(crate) fn new() -> Self {
14        Self { id: Cell::new(0) }
15    }
16
17    /// Get the next id.
18    pub(crate) fn next(&self) -> NonZeroId {
19        let cur = self.id.get();
20        let id = cur
21            .checked_add(1)
22            .and_then(NonZeroU32::new)
23            .expect("ran out of ids");
24        self.id.set(id.get());
25        NonZeroId::from(id)
26    }
27}
28
29impl Default for Gen {
30    fn default() -> Self {
31        Self::new()
32    }
33}