tola_caps/trie/
aliases.rs

1//! Convenience types and macros for capability sets
2//!
3//! Provides type aliases and macros for easier capability set construction.
4
5use super::node::Empty;
6use super::insert::With;
7
8// =============================================================================
9// Convenience Type Aliases
10// =============================================================================
11
12/// Empty capability set
13pub type CapSet0 = Empty;
14
15/// Capability set with 1 capability
16pub type CapSet1<A> = <Empty as With<A>>::Out;
17
18/// Capability set with 2 capabilities
19pub type CapSet2<A, B> = <<Empty as With<A>>::Out as With<B>>::Out;
20
21/// Capability set with 3 capabilities
22pub type CapSet3<A, B, C> = <<<Empty as With<A>>::Out as With<B>>::Out as With<C>>::Out;
23
24/// Capability set with 4 capabilities
25pub type CapSet4<A, B, C, D> = <<<<Empty as With<A>>::Out as With<B>>::Out as With<C>>::Out as With<D>>::Out;
26
27// =============================================================================
28// Convenience Macros
29// =============================================================================
30
31// Note: hlist!, all!, any! are defined in evaluate.rs
32
33/// Macro to build a CapSet from multiple capabilities
34/// Usage: `capset![CapA, CapB, CapC]`
35#[macro_export]
36macro_rules! capset {
37    () => { $crate::trie::Empty };
38    ($cap:ty) => {
39        <$crate::trie::Empty as $crate::trie::With<$cap>>::Out
40    };
41    ($cap:ty, $($rest:ty),+ $(,)?) => {
42        <$crate::capset![$($rest),+] as $crate::trie::With<$cap>>::Out
43    };
44}
45
46/// Macro to compute union of two capability sets
47/// Usage: `union![SetA, SetB]`
48#[macro_export]
49macro_rules! union {
50    ($a:ty, $b:ty) => {
51        <$a as $crate::trie::SetUnion<$b>>::Out
52    };
53}
54
55/// Macro to compute intersection of two capability sets
56/// Usage: `intersect![SetA, SetB]`
57#[macro_export]
58macro_rules! intersect {
59    ($a:ty, $b:ty) => {
60        <$a as $crate::trie::SetIntersect<$b>>::Out
61    };
62}
63
64/// Macro to add a capability to a set
65/// Usage:
66/// - `with![Set, Cap]` -> Single add
67/// - `with![Set, A, B]` -> Chain add (A then B)
68/// - `with![Cap]` -> Implicit `__C`
69#[macro_export]
70macro_rules! with {
71    ($cap:ty) => {
72        <__C as $crate::trie::With<$cap>>::Out
73    };
74    ($set:ty, $cap:ty) => {
75        <$set as $crate::trie::With<$cap>>::Out
76    };
77    ($set:ty, $cap:ty, $($rest:ty),+) => {
78        $crate::with![ $crate::with![$set, $cap], $($rest),+ ]
79    };
80}
81
82/// Macro to remove a capability from a set
83/// Usage:
84/// - `without![Set, Cap]` -> Remove Cap from Set
85/// - `without![Set, A, B]` -> Remove A then B
86/// - `without![Cap]` -> Remove Cap from implicit `__C`
87#[macro_export]
88macro_rules! without {
89    ($cap:ty) => {
90        <__C as $crate::trie::Without<$cap>>::Out
91    };
92    ($set:ty, $cap:ty) => {
93        <$set as $crate::trie::Without<$cap>>::Out
94    };
95    ($set:ty, $cap:ty, $($rest:ty),+) => {
96        $crate::without![ $crate::without![$set, $cap], $($rest),+ ]
97    };
98}