sel4/
cap_rights.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
//
// Copyright 2023, Colias Group, LLC
//
// SPDX-License-Identifier: MIT
//

use crate::{newtype_methods, sys};

/// Corresponds to `seL4_CapRights_t`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CapRights(sys::seL4_CapRights);

impl CapRights {
    newtype_methods!(pub sys::seL4_CapRights);

    pub fn new(grant_reply: bool, grant: bool, read: bool, write: bool) -> Self {
        Self::from_inner(sys::seL4_CapRights::new(
            grant_reply.into(),
            grant.into(),
            read.into(),
            write.into(),
        ))
    }

    pub fn none() -> Self {
        CapRightsBuilder::none().build()
    }

    pub fn all() -> Self {
        CapRightsBuilder::all().build()
    }

    pub fn read_write() -> Self {
        CapRightsBuilder::none().read(true).write(true).build()
    }

    pub fn read_only() -> Self {
        CapRightsBuilder::none().read(true).build()
    }

    pub fn write_only() -> Self {
        CapRightsBuilder::none().write(true).build()
    }
}

impl From<CapRightsBuilder> for CapRights {
    fn from(builder: CapRightsBuilder) -> Self {
        builder.build()
    }
}

/// Helper for constructing [`CapRights`].
#[derive(Debug, Copy, Clone, PartialEq, Eq, Default)]
pub struct CapRightsBuilder {
    grant_reply: bool,
    grant: bool,
    read: bool,
    write: bool,
}

impl CapRightsBuilder {
    pub fn none() -> Self {
        Default::default()
    }

    pub fn all() -> Self {
        Self {
            grant_reply: true,
            grant: true,
            read: true,
            write: true,
        }
    }

    pub fn build(self) -> CapRights {
        CapRights::new(self.grant_reply, self.grant, self.read, self.write)
    }

    #[must_use]
    pub fn grant_reply(mut self, can: bool) -> Self {
        self.grant_reply = can;
        self
    }

    #[must_use]
    pub fn grant(mut self, can: bool) -> Self {
        self.grant = can;
        self
    }

    #[must_use]
    pub fn read(mut self, can: bool) -> Self {
        self.read = can;
        self
    }

    #[must_use]
    pub fn write(mut self, can: bool) -> Self {
        self.write = can;
        self
    }
}