1use std::any;
2use std::fmt::{Debug, Display, Error, Formatter};
3use std::marker::PhantomData;
4
5use serde::{Deserialize, Serialize};
6
7#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
8pub struct Id<M, V = usize> {
9 inner: V,
10 _phantom: PhantomData<M>,
11}
12
13impl<M, V> Id<M, V> {
14 #[inline]
19 pub const fn new(inner: V) -> Self {
20 Self {
21 inner,
22 _phantom: PhantomData,
23 }
24 }
25}
26
27impl<M, V> Id<M, V>
28where
29 V: Copy,
30{
31 #[inline]
35 pub const fn get(self) -> V {
36 self.inner
37 }
38}
39
40impl<M> Id<M, usize> {
41 pub const ZERO: Self = Self::new(0);
42}
43
44impl<M, V> Debug for Id<M, V>
45where
46 V: Display,
47{
48 fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
49 let marker = any::type_name::<M>().split("::").last().expect("not empty");
50 write!(f, "{marker}Id({})", self.inner)
51 }
52}
53
54impl<M, V> Serialize for Id<M, V>
55where
56 V: Serialize,
57{
58 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
59 where
60 S: serde::Serializer,
61 {
62 self.inner.serialize(serializer)
63 }
64}
65
66impl<'de, M, V> Deserialize<'de> for Id<M, V>
67where
68 V: Deserialize<'de>,
69{
70 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
71 where
72 D: serde::Deserializer<'de>,
73 {
74 let inner = V::deserialize(deserializer)?;
75 Ok(Self {
76 inner,
77 _phantom: PhantomData,
78 })
79 }
80}
81
82pub mod marker {
83 #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
84 pub struct Var;
85 #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
86 pub struct Decl;
87 #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
88 pub struct Block;
89 #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
90 pub struct Module;
91 #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
92 pub struct TypeAlias;
93 #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
94 pub struct Overlay;
95 #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
96 pub struct File;
97 #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
98 pub struct VirtualPath;
99 #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
100 pub struct Span;
101 #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
102 pub struct Reg;
103 #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
104 pub struct Job;
105}
106
107pub type VarId = Id<marker::Var>;
108pub type DeclId = Id<marker::Decl>;
109pub type BlockId = Id<marker::Block>;
110pub type ModuleId = Id<marker::Module>;
111pub type TypeAliasId = Id<marker::TypeAlias>;
112pub type OverlayId = Id<marker::Overlay>;
113pub type FileId = Id<marker::File>;
114pub type VirtualPathId = Id<marker::VirtualPath>;
115pub type SpanId = Id<marker::Span>;
116pub type JobId = Id<marker::Job>;
117
118pub type RegId = Id<marker::Reg, u32>;
124
125impl Display for JobId {
126 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
127 write!(f, "{}", self.inner)
128 }
129}
130
131impl Display for RegId {
132 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
133 write!(f, "%{}", self.get())
134 }
135}