sel4/
helper_macros.rs

1//
2// Copyright 2023, Colias Group, LLC
3//
4// SPDX-License-Identifier: MIT
5//
6
7macro_rules! newtype_methods {
8    ($inner_vis:vis $inner:path) => {
9        $inner_vis const fn from_inner(inner: $inner) -> Self {
10            Self(inner)
11        }
12
13        $inner_vis const fn into_inner(self) -> $inner {
14            self.0
15        }
16
17        $inner_vis const fn inner(&self) -> &$inner {
18            &self.0
19        }
20
21        $inner_vis fn inner_mut(&mut self) -> &mut $inner {
22            &mut self.0
23        }
24    };
25}
26
27macro_rules! declare_cap_type {
28    (
29        $(#[$outer:meta])*
30        $t:ident
31    ) => {
32        $(#[$outer])*
33        #[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
34        pub struct $t;
35
36        impl $crate::CapType for $t {
37            const NAME: &'static str = stringify!($t);
38        }
39    };
40}
41
42macro_rules! declare_cap_type_for_object {
43    (
44        $(#[$outer:meta])*
45        $t:ident { $object_type:ident }
46    ) => {
47        $crate::declare_cap_type! {
48            $(#[$outer])*
49            $t
50        }
51
52        impl $crate::CapTypeForObject for $t {
53            fn object_type() -> $crate::ObjectType {
54                $crate::$object_type::$t.into()
55            }
56        }
57    };
58}
59
60macro_rules! declare_cap_type_for_object_of_fixed_size {
61    (
62        $(#[$outer:meta])*
63        $t:ident { $object_type:ident, $object_blueprint:ident }
64    ) => {
65        $crate::declare_cap_type_for_object! {
66            $(#[$outer])*
67            $t { $object_type }
68        }
69
70        impl $crate::CapTypeForObjectOfFixedSize for $t {
71            fn object_blueprint() -> $crate::ObjectBlueprint {
72                $crate::$object_blueprint::$t.into()
73            }
74        }
75    };
76}
77
78macro_rules! declare_cap_type_for_object_of_variable_size {
79    (
80        $(#[$outer:meta])*
81        $t:ident { $object_type:ident, $object_blueprint:ident }
82    ) => {
83        $crate::declare_cap_type_for_object! {
84            $(#[$outer])*
85            $t { $object_type }
86        }
87
88        impl $crate::CapTypeForObjectOfVariableSize for $t {
89            fn object_blueprint(size_bits: usize) -> $crate::ObjectBlueprint {
90                ($crate::$object_blueprint::$t { size_bits }).into()
91            }
92        }
93    };
94}
95
96macro_rules! declare_cap_alias {
97    (
98        $(#[$outer:meta])*
99        $t:ident
100    ) => {
101        $(#[$outer])*
102        pub type $t<C = $crate::NoExplicitInvocationContext> =
103            $crate::Cap<$crate::cap_type::$t, C>;
104    };
105}
106
107macro_rules! declare_fault_newtype {
108    ($t:ident, $sys:ident) => {
109        #[doc = "Corresponds to `"]
110        #[doc = stringify!($sys)]
111        #[doc = "`."]
112        #[derive(Debug, Clone, PartialEq, Eq)]
113        pub struct $t($crate::sys::$sys);
114
115        impl $t {
116            $crate::newtype_methods!(pub $crate::sys::$sys);
117        }
118    };
119}
120
121pub(crate) use declare_cap_alias;
122pub(crate) use declare_cap_type;
123pub(crate) use declare_cap_type_for_object;
124pub(crate) use declare_cap_type_for_object_of_fixed_size;
125pub(crate) use declare_cap_type_for_object_of_variable_size;
126pub(crate) use declare_fault_newtype;
127pub(crate) use newtype_methods;