plotnik_bytecode/bytecode/ids.rs
1//! Bytecode index newtypes.
2
3use std::num::NonZeroU16;
4
5/// Index into the String Table.
6///
7/// Uses NonZeroU16 to make StringId(0) unrepresentable - index 0 is
8/// reserved for the easter egg and never referenced by instructions.
9#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
10#[repr(transparent)]
11pub struct StringId(pub NonZeroU16);
12
13impl StringId {
14 /// Create a new StringId. Panics if n == 0.
15 #[inline]
16 pub fn new(n: u16) -> Self {
17 Self(NonZeroU16::new(n).expect("StringId cannot be 0"))
18 }
19
20 /// Get the raw u16 value.
21 #[inline]
22 pub fn get(self) -> u16 {
23 self.0.get()
24 }
25}
26
27/// Index into the Type Definition table.
28/// All types (including builtins) are stored sequentially in TypeDefs.
29#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, Default)]
30#[repr(transparent)]
31pub struct TypeId(pub u16);