spacetimedb_sats/
size_of.rs

1use ethnum::{i256, u256};
2
3use crate::{algebraic_value::Packed, AlgebraicValue, ArrayValue, ProductValue, SumValue, F32, F64};
4
5pub trait SizeOf {
6    /// Returns the unpadded size in bytes of an [AlgebraicValue] or primitive
7    fn size_of(&self) -> usize;
8}
9
10macro_rules! impl_size_of_primitive {
11    ($prim:ty) => {
12        impl SizeOf for $prim {
13            fn size_of(&self) -> usize {
14                std::mem::size_of::<Self>()
15            }
16        }
17    };
18    ($($prim:ty,)*) => {
19        $(impl_size_of_primitive!($prim);)*
20    };
21}
22
23impl_size_of_primitive!(
24    bool,
25    u8,
26    i8,
27    u16,
28    i16,
29    u32,
30    i32,
31    u64,
32    i64,
33    u128,
34    i128,
35    Packed<u128>,
36    Packed<i128>,
37    u256,
38    i256,
39    F32,
40    F64,
41);
42
43impl SizeOf for Box<str> {
44    fn size_of(&self) -> usize {
45        self.len()
46    }
47}
48
49impl SizeOf for AlgebraicValue {
50    fn size_of(&self) -> usize {
51        match self {
52            Self::Min | Self::Max => unreachable!(),
53            Self::String(x) => x.size_of(),
54            Self::Bool(x) => x.size_of(),
55            Self::U8(x) => x.size_of(),
56            Self::I8(x) => x.size_of(),
57            Self::U16(x) => x.size_of(),
58            Self::I16(x) => x.size_of(),
59            Self::U32(x) => x.size_of(),
60            Self::I32(x) => x.size_of(),
61            Self::U64(x) => x.size_of(),
62            Self::I64(x) => x.size_of(),
63            Self::U128(x) => x.size_of(),
64            Self::I128(x) => x.size_of(),
65            Self::U256(x) => x.size_of(),
66            Self::I256(x) => x.size_of(),
67            Self::F32(x) => x.size_of(),
68            Self::F64(x) => x.size_of(),
69            Self::Sum(x) => x.size_of(),
70            Self::Product(x) => x.size_of(),
71            Self::Array(x) => x.size_of(),
72        }
73    }
74}
75
76impl SizeOf for SumValue {
77    fn size_of(&self) -> usize {
78        1 + self.value.size_of()
79    }
80}
81
82impl SizeOf for ProductValue {
83    fn size_of(&self) -> usize {
84        self.elements.size_of()
85    }
86}
87
88impl<T> SizeOf for [T]
89where
90    T: SizeOf,
91{
92    fn size_of(&self) -> usize {
93        self.iter().map(|elt| elt.size_of()).sum()
94    }
95}
96
97impl SizeOf for ArrayValue {
98    fn size_of(&self) -> usize {
99        match self {
100            Self::Sum(elts) => elts.size_of(),
101            Self::Product(elts) => elts.size_of(),
102            Self::Bool(elts) => elts.size_of(),
103            Self::I8(elts) => elts.size_of(),
104            Self::U8(elts) => elts.size_of(),
105            Self::I16(elts) => elts.size_of(),
106            Self::U16(elts) => elts.size_of(),
107            Self::I32(elts) => elts.size_of(),
108            Self::U32(elts) => elts.size_of(),
109            Self::I64(elts) => elts.size_of(),
110            Self::U64(elts) => elts.size_of(),
111            Self::I128(elts) => elts.size_of(),
112            Self::U128(elts) => elts.size_of(),
113            Self::I256(elts) => elts.size_of(),
114            Self::U256(elts) => elts.size_of(),
115            Self::F32(elts) => elts.size_of(),
116            Self::F64(elts) => elts.size_of(),
117            Self::String(elts) => elts.size_of(),
118            Self::Array(elts) => elts.size_of(),
119        }
120    }
121}