Skip to main content

pedant_types/resolution/
id.rs

1//! Dense report identifiers branded by the collection they index.
2
3use std::marker::PhantomData;
4
5use serde::{Deserialize, Serialize};
6
7/// One dense `u32` index whose marker fixes the collection it addresses.
8#[derive(Serialize, Deserialize, Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
9#[repr(transparent)]
10#[serde(transparent)]
11pub struct Id<K> {
12    index: u32,
13    #[serde(skip)]
14    kind: PhantomData<fn() -> K>,
15}
16
17impl<K> Id<K> {
18    /// The identifier's position in its report slice.
19    pub fn index(self) -> u32 {
20        self.index
21    }
22
23    pub(crate) fn new(index: u32) -> Self {
24        Self {
25            index,
26            kind: PhantomData,
27        }
28    }
29}
30
31/// Marker distinguishing unit identifiers from the other ID kinds.
32#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
33pub struct UnitIdKind;
34
35/// Marker distinguishing definition identifiers from the other ID kinds.
36#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
37pub struct DefinitionIdKind;
38
39/// Marker distinguishing reference identifiers from the other ID kinds.
40#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
41pub struct ReferenceIdKind;
42
43/// A resolution unit's dense identifier within one report.
44pub type ResolutionUnitId = Id<UnitIdKind>;
45
46/// A definition's dense identifier within one report.
47pub type DefinitionId = Id<DefinitionIdKind>;
48
49/// A reference's dense identifier within one report.
50pub type ReferenceId = Id<ReferenceIdKind>;