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

use crate::{newtype_methods, sys, Word};

/// Corresponds to `seL4_MessageInfo_t`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MessageInfo(sys::seL4_MessageInfo);

impl MessageInfo {
    newtype_methods!(pub sys::seL4_MessageInfo);

    pub fn new(label: Word, caps_unwrapped: usize, extra_caps: usize, length: usize) -> Self {
        Self::from_inner(sys::seL4_MessageInfo::new(
            label,
            caps_unwrapped.try_into().unwrap(),
            extra_caps.try_into().unwrap(),
            length.try_into().unwrap(),
        ))
    }

    pub fn label(&self) -> Word {
        self.inner().get_label()
    }

    pub const fn label_width() -> usize {
        sys::seL4_MessageInfo::width_of_label()
    }

    pub fn caps_unwrapped(&self) -> usize {
        self.inner().get_capsUnwrapped().try_into().unwrap()
    }

    pub fn extra_caps(&self) -> usize {
        self.inner().get_extraCaps().try_into().unwrap()
    }

    pub fn length(&self) -> usize {
        self.inner().get_length().try_into().unwrap()
    }
}

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

/// Helper for constructing [`MessageInfo`].
#[derive(Debug, Copy, Clone, PartialEq, Eq, Default)]
pub struct MessageInfoBuilder {
    label: Word,
    caps_unwrapped: usize,
    extra_caps: usize,
    length: usize,
}

impl MessageInfoBuilder {
    pub fn build(self) -> MessageInfo {
        MessageInfo::new(
            self.label,
            self.caps_unwrapped,
            self.extra_caps,
            self.length,
        )
    }

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

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

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

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