sel4/
cap_rights.rs

1//
2// Copyright 2023, Colias Group, LLC
3//
4// SPDX-License-Identifier: MIT
5//
6
7use crate::{newtype_methods, sys};
8
9/// Corresponds to `seL4_CapRights_t`.
10#[derive(Debug, Clone, PartialEq, Eq)]
11pub struct CapRights(sys::seL4_CapRights);
12
13impl CapRights {
14    newtype_methods!(pub sys::seL4_CapRights);
15
16    pub fn new(grant_reply: bool, grant: bool, read: bool, write: bool) -> Self {
17        Self::from_inner(sys::seL4_CapRights::new(
18            grant_reply.into(),
19            grant.into(),
20            read.into(),
21            write.into(),
22        ))
23    }
24
25    pub fn none() -> Self {
26        CapRightsBuilder::none().build()
27    }
28
29    pub fn all() -> Self {
30        CapRightsBuilder::all().build()
31    }
32
33    pub fn read_write() -> Self {
34        CapRightsBuilder::none().read(true).write(true).build()
35    }
36
37    pub fn read_only() -> Self {
38        CapRightsBuilder::none().read(true).build()
39    }
40
41    pub fn write_only() -> Self {
42        CapRightsBuilder::none().write(true).build()
43    }
44}
45
46impl From<CapRightsBuilder> for CapRights {
47    fn from(builder: CapRightsBuilder) -> Self {
48        builder.build()
49    }
50}
51
52/// Helper for constructing [`CapRights`].
53#[derive(Debug, Copy, Clone, PartialEq, Eq, Default)]
54pub struct CapRightsBuilder {
55    grant_reply: bool,
56    grant: bool,
57    read: bool,
58    write: bool,
59}
60
61impl CapRightsBuilder {
62    pub fn none() -> Self {
63        Default::default()
64    }
65
66    pub fn all() -> Self {
67        Self {
68            grant_reply: true,
69            grant: true,
70            read: true,
71            write: true,
72        }
73    }
74
75    pub fn build(self) -> CapRights {
76        CapRights::new(self.grant_reply, self.grant, self.read, self.write)
77    }
78
79    #[must_use]
80    pub fn grant_reply(mut self, can: bool) -> Self {
81        self.grant_reply = can;
82        self
83    }
84
85    #[must_use]
86    pub fn grant(mut self, can: bool) -> Self {
87        self.grant = can;
88        self
89    }
90
91    #[must_use]
92    pub fn read(mut self, can: bool) -> Self {
93        self.read = can;
94        self
95    }
96
97    #[must_use]
98    pub fn write(mut self, can: bool) -> Self {
99        self.write = can;
100        self
101    }
102}