yamakan/
generators.rs

1//! Observation identifier generators.
2use crate::{IdGen, ObsId, Result};
3
4/// An implementation of `IdGen` that generates serial identifiers starting from zero.
5#[derive(Debug, Default)]
6pub struct SerialIdGenerator {
7    next_id: u64,
8}
9impl SerialIdGenerator {
10    /// Makes a new `SerialIdGenerator` instance.
11    pub const fn new() -> Self {
12        Self { next_id: 0 }
13    }
14}
15impl IdGen for SerialIdGenerator {
16    fn generate(&mut self) -> Result<ObsId> {
17        let id = self.next_id;
18        self.next_id += 1;
19        Ok(ObsId::new(id))
20    }
21}
22
23/// An implementation of `IdGen` that always returns the same identifier.
24#[derive(Debug)]
25pub struct ConstIdGenerator {
26    id: ObsId,
27}
28impl ConstIdGenerator {
29    /// Makes a new `ConstIdGenerator` instance.
30    ///
31    /// When `ConstIdGenerator::generate` method is called, it always returns the given identifier.
32    pub const fn new(id: ObsId) -> Self {
33        Self { id }
34    }
35}
36impl IdGen for ConstIdGenerator {
37    fn generate(&mut self) -> Result<ObsId> {
38        Ok(self.id)
39    }
40}