rune/shared/
gen.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
use core::cell::Cell;
use core::num::NonZeroU32;

use crate::parse::NonZeroId;

#[derive(Debug)]
pub(crate) struct Gen {
    id: Cell<u32>,
}

impl Gen {
    /// Construct a new shared generator.
    pub(crate) fn new() -> Self {
        Self { id: Cell::new(0) }
    }

    /// Get the next id.
    pub(crate) fn next(&self) -> NonZeroId {
        let cur = self.id.get();
        let id = cur
            .checked_add(1)
            .and_then(NonZeroU32::new)
            .expect("ran out of ids");
        self.id.set(id.get());
        NonZeroId::from(id)
    }
}

impl Default for Gen {
    fn default() -> Self {
        Self::new()
    }
}