hydrate_pipeline/
uuid_newtype.rs1#[macro_export]
2macro_rules! create_uuid_newtype {
3 ($data_type:ident, $name:literal) => {
4 #[derive(
5 Copy, Clone, PartialEq, Eq, Hash, Ord, PartialOrd, serde::Serialize, serde::Deserialize,
6 )]
7 pub struct $data_type(u128);
8 impl $data_type {
9 pub const fn null() -> Self {
10 Self(0)
11 }
12
13 pub fn from_bytes(bytes: [u8; 16]) -> Self {
14 Self(uuid::Uuid::from_bytes(bytes).as_u128())
15 }
16
17 pub fn as_bytes(self) -> [u8; 16] {
18 *uuid::Uuid::from_u128(self.0).as_bytes()
19 }
20
21 pub fn from_uuid(uuid: uuid::Uuid) -> Self {
22 Self(uuid.as_u128())
23 }
24
25 pub fn as_uuid(self) -> uuid::Uuid {
26 uuid::Uuid::from_u128(self.0)
27 }
28
29 pub fn from_u128(u: u128) -> Self {
30 Self(u)
31 }
32
33 pub fn as_u128(self) -> u128 {
34 self.0
35 }
36
37 pub fn is_null(&self) -> bool {
38 return self.0 == 0;
39 }
40 }
41
42 impl std::fmt::Debug for $data_type {
43 fn fmt(
44 &self,
45 f: &mut std::fmt::Formatter<'_>,
46 ) -> std::fmt::Result {
47 f.debug_tuple($name)
48 .field(&uuid::Uuid::from_u128(self.0))
49 .finish()
50 }
51 }
52 };
53}