reifydb_type/value/
sumtype.rs1use std::{
5 fmt,
6 fmt::{Display, Formatter},
7 ops::Deref,
8};
9
10use serde::{Deserialize, Deserializer, Serialize, Serializer, de::Visitor};
11
12#[repr(transparent)]
13#[derive(Debug, Copy, Clone, PartialOrd, PartialEq, Ord, Eq, Hash)]
14pub struct SumTypeId(pub u64);
15
16#[derive(Debug, Copy, Clone, PartialOrd, PartialEq, Ord, Eq, Hash, Serialize, Deserialize)]
17pub struct VariantRef {
18 pub sumtype_id: SumTypeId,
19 pub variant_tag: u8,
20}
21
22impl Display for SumTypeId {
23 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
24 Display::fmt(&self.0, f)
25 }
26}
27
28impl Deref for SumTypeId {
29 type Target = u64;
30
31 fn deref(&self) -> &Self::Target {
32 &self.0
33 }
34}
35
36impl PartialEq<u64> for SumTypeId {
37 fn eq(&self, other: &u64) -> bool {
38 self.0.eq(other)
39 }
40}
41
42impl From<SumTypeId> for u64 {
43 fn from(value: SumTypeId) -> Self {
44 value.0
45 }
46}
47
48impl SumTypeId {
49 #[inline]
50 pub fn to_u64(self) -> u64 {
51 self.0
52 }
53}
54
55impl From<i32> for SumTypeId {
56 fn from(value: i32) -> Self {
57 Self(value as u64)
58 }
59}
60
61impl From<u64> for SumTypeId {
62 fn from(value: u64) -> Self {
63 Self(value)
64 }
65}
66
67impl Serialize for SumTypeId {
68 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
69 where
70 S: Serializer,
71 {
72 serializer.serialize_u64(self.0)
73 }
74}
75
76impl<'de> Deserialize<'de> for SumTypeId {
77 fn deserialize<D>(deserializer: D) -> Result<SumTypeId, D::Error>
78 where
79 D: Deserializer<'de>,
80 {
81 struct U64Visitor;
82
83 impl Visitor<'_> for U64Visitor {
84 type Value = SumTypeId;
85
86 fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
87 formatter.write_str("an unsigned 64-bit number")
88 }
89
90 fn visit_u64<E>(self, value: u64) -> Result<Self::Value, E> {
91 Ok(SumTypeId(value))
92 }
93 }
94
95 deserializer.deserialize_u64(U64Visitor)
96 }
97}