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
16impl Display for SumTypeId {
17 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
18 Display::fmt(&self.0, f)
19 }
20}
21
22impl Deref for SumTypeId {
23 type Target = u64;
24
25 fn deref(&self) -> &Self::Target {
26 &self.0
27 }
28}
29
30impl PartialEq<u64> for SumTypeId {
31 fn eq(&self, other: &u64) -> bool {
32 self.0.eq(other)
33 }
34}
35
36impl From<SumTypeId> for u64 {
37 fn from(value: SumTypeId) -> Self {
38 value.0
39 }
40}
41
42impl SumTypeId {
43 #[inline]
44 pub fn to_u64(self) -> u64 {
45 self.0
46 }
47}
48
49impl From<i32> for SumTypeId {
50 fn from(value: i32) -> Self {
51 Self(value as u64)
52 }
53}
54
55impl From<u64> for SumTypeId {
56 fn from(value: u64) -> Self {
57 Self(value)
58 }
59}
60
61impl Serialize for SumTypeId {
62 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
63 where
64 S: Serializer,
65 {
66 serializer.serialize_u64(self.0)
67 }
68}
69
70impl<'de> Deserialize<'de> for SumTypeId {
71 fn deserialize<D>(deserializer: D) -> Result<SumTypeId, D::Error>
72 where
73 D: Deserializer<'de>,
74 {
75 struct U64Visitor;
76
77 impl Visitor<'_> for U64Visitor {
78 type Value = SumTypeId;
79
80 fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
81 formatter.write_str("an unsigned 64-bit number")
82 }
83
84 fn visit_u64<E>(self, value: u64) -> Result<Self::Value, E> {
85 Ok(SumTypeId(value))
86 }
87 }
88
89 deserializer.deserialize_u64(U64Visitor)
90 }
91}