Skip to main content

roam_types/
lib.rs

1macro_rules! declare_id {
2    ($(#[$meta:meta])* $name:ident, $inner:ty) => {
3        $(#[$meta])*
4        #[derive(Facet, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Clone, Copy)]
5        #[repr(transparent)]
6        #[facet(transparent)]
7        pub struct $name(pub $inner);
8
9        impl ::std::fmt::Display for $name {
10            fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
11                write!(f, "{}", self.0)
12            }
13        }
14
15        impl $name {
16            /// Returns `true` if this ID has the given parity (even or odd).
17            pub fn has_parity(self, parity: crate::Parity) -> bool {
18                match parity {
19                    crate::Parity::Even => self.0.is_multiple_of(2),
20                    crate::Parity::Odd => !self.0.is_multiple_of(2),
21                }
22            }
23        }
24
25        impl crate::IdType for $name {
26            fn from_raw(raw: u64) -> Self {
27                Self(raw as $inner)
28            }
29        }
30
31    };
32}
33
34/// Trait implemented by all `declare_id!` types, enabling generic ID allocation.
35pub trait IdType: Copy {
36    fn from_raw(raw: u64) -> Self;
37}
38
39/// Allocates IDs with a given parity (odd or even), stepping by 2.
40///
41/// Odd parity: 1, 3, 5, 7, ...
42/// Even parity: 2, 4, 6, 8, ...
43// r[impl rpc.request.id-allocation]
44pub struct IdAllocator<T: IdType> {
45    next: u64,
46    _phantom: std::marker::PhantomData<T>,
47}
48
49impl<T: IdType> IdAllocator<T> {
50    /// Create a new allocator for the given parity.
51    pub fn new(parity: Parity) -> Self {
52        let next = match parity {
53            Parity::Odd => 1,
54            Parity::Even => 2,
55        };
56        Self {
57            next,
58            _phantom: std::marker::PhantomData,
59        }
60    }
61
62    /// Allocate the next ID.
63    pub fn alloc(&mut self) -> T {
64        let id = T::from_raw(self.next);
65        self.next += 2;
66        id
67    }
68}
69
70mod rpc_plan;
71pub use rpc_plan::*;
72
73mod roam_error;
74pub use roam_error::*;
75
76mod services;
77pub use services::*;
78
79mod requests;
80pub use requests::*;
81
82mod message;
83pub use message::*;
84
85mod selfref;
86pub use selfref::*;
87
88mod link;
89pub use link::*;
90
91mod conduit;
92pub use conduit::*;
93
94mod metadata;
95pub use metadata::*;
96
97mod calls;
98pub use calls::*;
99
100mod channel;
101pub use channel::*;
102
103#[cfg(not(target_arch = "wasm32"))]
104mod channel_binding;
105#[cfg(not(target_arch = "wasm32"))]
106pub use channel_binding::*;
107
108mod shape_classify;
109pub use shape_classify::*;