1use core::fmt::Display;
2
3#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
4pub struct Capabilities(u8);
5
6impl Capabilities {
7 pub const EXECUTABLE_FLAG: u8 = 1 << 0;
8 pub const DIRECT_MEMORY_ACCESS_FLAG: u8 = 1 << 1;
9
10 pub const fn new(executable: bool, direct_memory_access: bool) -> Self {
11 Capabilities(0)
12 .set_executable(executable)
13 .set_direct_memory_access(direct_memory_access)
14 }
15
16 pub const fn get_executable(&self) -> bool {
17 self.0 & Capabilities::EXECUTABLE_FLAG != 0
18 }
19
20 pub const fn get_direct_memory_access(&self) -> bool {
21 self.0 & Capabilities::DIRECT_MEMORY_ACCESS_FLAG != 0
22 }
23
24 pub const fn set_executable(mut self, value: bool) -> Self {
25 if value {
26 self.0 |= Capabilities::EXECUTABLE_FLAG;
27 } else {
28 self.0 &= !Capabilities::EXECUTABLE_FLAG;
29 }
30 self
31 }
32
33 pub const fn set_direct_memory_access(mut self, value: bool) -> Self {
34 if value {
35 self.0 |= Capabilities::DIRECT_MEMORY_ACCESS_FLAG;
36 } else {
37 self.0 &= !Capabilities::DIRECT_MEMORY_ACCESS_FLAG;
38 }
39 self
40 }
41
42 pub const fn is_subset_of(&self, other: Capabilities) -> bool {
43 (self.0 & other.0) == self.0
44 }
45
46 pub const fn is_superset_of(&self, other: Capabilities) -> bool {
47 (self.0 & other.0) == other.0
48 }
49
50 pub const fn from_u8(value: u8) -> Self {
51 Capabilities(value)
52 }
53
54 pub const fn to_u8(&self) -> u8 {
55 self.0
56 }
57}
58
59impl Display for Capabilities {
60 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
61 write!(f, "Capabilities: ")?;
62 if self.get_executable() {
63 write!(f, "Executable ")?;
64 }
65 if self.get_direct_memory_access() {
66 write!(f, "Direct memory access ")?;
67 }
68 Ok(())
69 }
70}