spacetimedb_sats/
algebraic_type_ref.rs

1use crate::impl_st;
2use crate::{algebraic_type::AlgebraicType, impl_deserialize, impl_serialize, meta_type::MetaType};
3use std::fmt::Display;
4
5/// A reference to an [`AlgebraicType`] within a `Typespace`.
6///
7/// Using this in a different `Typespace` than its maker
8/// will most likely result in a panic.
9#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)]
10pub struct AlgebraicTypeRef(
11    /// The index into the specific `Typespace`'s list of types.
12    pub u32,
13);
14
15impl AlgebraicTypeRef {
16    /// Returns the index into the specific `Typespace`'s list of types.
17    pub const fn idx(self) -> usize {
18        self.0 as usize
19    }
20}
21
22impl_serialize!([] AlgebraicTypeRef, (self, ser) => self.0.serialize(ser));
23impl_deserialize!([] AlgebraicTypeRef, de => u32::deserialize(de).map(Self));
24impl_st!([] AlgebraicTypeRef, AlgebraicType::U32);
25
26impl Display for AlgebraicTypeRef {
27    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
28        // For example: `&42`.
29        write!(f, "&{}", self.0)
30    }
31}
32
33impl MetaType for AlgebraicTypeRef {
34    fn meta_type() -> AlgebraicType {
35        AlgebraicType::U32
36    }
37}