sel4/
helper_macros.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
//
// Copyright 2023, Colias Group, LLC
//
// SPDX-License-Identifier: MIT
//

macro_rules! newtype_methods {
    ($inner_vis:vis $inner:path) => {
        $inner_vis const fn from_inner(inner: $inner) -> Self {
            Self(inner)
        }

        $inner_vis const fn into_inner(self) -> $inner {
            self.0
        }

        $inner_vis const fn inner(&self) -> &$inner {
            &self.0
        }

        $inner_vis fn inner_mut(&mut self) -> &mut $inner {
            &mut self.0
        }
    };
}

macro_rules! declare_cap_type {
    (
        $(#[$outer:meta])*
        $t:ident
    ) => {
        $(#[$outer])*
        #[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
        pub struct $t;

        impl $crate::CapType for $t {
            const NAME: &'static str = stringify!($t);
        }
    };
}

macro_rules! declare_cap_type_for_object {
    (
        $(#[$outer:meta])*
        $t:ident { $object_type:ident }
    ) => {
        $crate::declare_cap_type! {
            $(#[$outer])*
            $t
        }

        impl $crate::CapTypeForObject for $t {
            fn object_type() -> $crate::ObjectType {
                $crate::$object_type::$t.into()
            }
        }
    };
}

macro_rules! declare_cap_type_for_object_of_fixed_size {
    (
        $(#[$outer:meta])*
        $t:ident { $object_type:ident, $object_blueprint:ident }
    ) => {
        $crate::declare_cap_type_for_object! {
            $(#[$outer])*
            $t { $object_type }
        }

        impl $crate::CapTypeForObjectOfFixedSize for $t {
            fn object_blueprint() -> $crate::ObjectBlueprint {
                $crate::$object_blueprint::$t.into()
            }
        }
    };
}

macro_rules! declare_cap_type_for_object_of_variable_size {
    (
        $(#[$outer:meta])*
        $t:ident { $object_type:ident, $object_blueprint:ident }
    ) => {
        $crate::declare_cap_type_for_object! {
            $(#[$outer])*
            $t { $object_type }
        }

        impl $crate::CapTypeForObjectOfVariableSize for $t {
            fn object_blueprint(size_bits: usize) -> $crate::ObjectBlueprint {
                ($crate::$object_blueprint::$t { size_bits }).into()
            }
        }
    };
}

macro_rules! declare_cap_alias {
    (
        $(#[$outer:meta])*
        $t:ident
    ) => {
        $(#[$outer])*
        pub type $t<C = $crate::NoExplicitInvocationContext> =
            $crate::Cap<$crate::cap_type::$t, C>;
    };
}

macro_rules! declare_fault_newtype {
    ($t:ident, $sys:ident) => {
        #[doc = "Corresponds to `"]
        #[doc = stringify!($sys)]
        #[doc = "`."]
        #[derive(Debug, Clone, PartialEq, Eq)]
        pub struct $t($crate::sys::$sys);

        impl $t {
            $crate::newtype_methods!(pub $crate::sys::$sys);
        }
    };
}

pub(crate) use declare_cap_alias;
pub(crate) use declare_cap_type;
pub(crate) use declare_cap_type_for_object;
pub(crate) use declare_cap_type_for_object_of_fixed_size;
pub(crate) use declare_cap_type_for_object_of_variable_size;
pub(crate) use declare_fault_newtype;
pub(crate) use newtype_methods;