memory/
protection.rs

1//! Memory protection flags and operations.
2//!
3//! This module provides functionality to represent and manipulate memory protection
4//! settings (read, write, execute) and interfaces for applying these protections.
5
6use core::fmt::Debug;
7
8/// Represents memory protection flags.
9///
10/// This structure encapsulates read, write, and execute permissions for memory regions
11/// in a compact bit representation.
12#[derive(Clone, Copy, PartialEq, Eq)]
13#[repr(transparent)]
14pub struct Protection(u8);
15
16impl Debug for Protection {
17    fn fmt(&self, formatter: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
18        formatter
19            .debug_struct("Protection_type")
20            .field("Read", &self.get_read())
21            .field("Write", &self.get_write())
22            .field("Execute", &self.get_execute())
23            .finish()
24    }
25}
26
27impl Protection {
28    /// Bit flag representing read permission.
29    pub const READ_BIT: u8 = 1 << 0;
30
31    /// Bit flag representing write permission.
32    pub const WRITE_BIT: u8 = 1 << 1;
33
34    /// Bit flag representing execute permission.
35    pub const EXECUTE_BIT: u8 = 1 << 2;
36
37    /// No memory access permissions.
38    pub const NONE: Self = Self(0);
39
40    /// Read-only memory access.
41    pub const READ: Self = Self(Self::READ_BIT);
42
43    /// Write-only memory access.
44    pub const WRITE: Self = Self(Self::WRITE_BIT);
45
46    /// Execute-only memory access.
47    pub const EXECUTE: Self = Self(Self::EXECUTE_BIT);
48
49    /// Read and write memory access.
50    pub const READ_WRITE: Self = Self(Self::READ_BIT | Self::WRITE_BIT);
51
52    /// Read and execute memory access.
53    pub const READ_EXECUTE: Self = Self(Self::READ_BIT | Self::EXECUTE_BIT);
54
55    /// Write and execute memory access.
56    pub const WRITE_EXECUTE: Self = Self(Self::WRITE_BIT | Self::EXECUTE_BIT);
57
58    /// Full memory access (read, write, and execute).
59    pub const READ_WRITE_EXECUTE: Self = Self(Self::READ_BIT | Self::WRITE_BIT | Self::EXECUTE_BIT);
60
61    /// Creates a new protection type with specified permissions.
62    ///
63    /// # Parameters
64    /// - `Read`: Whether read permission is granted
65    /// - `Write`: Whether write permission is granted
66    /// - `Execute`: Whether execute permission is granted
67    ///
68    /// # Returns
69    /// A new protection type with the specified permissions.
70    pub const fn new(read: bool, write: bool, execute: bool) -> Self {
71        Self(0).set_read(read).set_write(write).set_execute(execute)
72    }
73
74    /// Sets or clears specified bits in the protection flags.
75    ///
76    /// # Parameters
77    /// - `Bits`: The bits to modify
78    /// - `Value`: Whether to set or clear the bits
79    ///
80    /// # Returns
81    /// A new protection type with the modified bits.
82    const fn set_bits(mut self, bits: u8, value: bool) -> Self {
83        if value {
84            self.0 |= bits;
85        } else {
86            self.0 &= !bits;
87        }
88        self
89    }
90
91    /// Checks if any of the specified bits are set.
92    ///
93    /// # Parameters
94    /// - `Bits`: The bits to check
95    ///
96    /// # Returns
97    /// `true` if any of the specified bits are set, `false` otherwise.
98    const fn get_bits(&self, bits: u8) -> bool {
99        self.0 & bits != 0
100    }
101
102    /// Sets or clears the read permission.
103    ///
104    /// # Parameters
105    /// - `Value`: Whether to grant or revoke read permission
106    ///
107    /// # Returns
108    /// A new protection type with the modified read permission.
109    pub const fn set_read(self, value: bool) -> Self {
110        self.set_bits(Self::READ_BIT, value)
111    }
112
113    /// Sets or clears the write permission.
114    ///
115    /// # Parameters
116    /// - `Value`: Whether to grant or revoke write permission
117    ///
118    /// # Returns
119    /// A new protection type with the modified write permission.
120    pub const fn set_write(self, value: bool) -> Self {
121        self.set_bits(Self::WRITE_BIT, value)
122    }
123
124    /// Sets or clears the execute permission.
125    ///
126    /// # Parameters
127    /// - `Value`: Whether to grant or revoke execute permission
128    ///
129    /// # Returns
130    /// A new protection type with the modified execute permission.
131    pub const fn set_execute(self, value: bool) -> Self {
132        self.set_bits(Self::EXECUTE_BIT, value)
133    }
134
135    /// Checks if read permission is granted.
136    ///
137    /// # Returns
138    /// `true` if read permission is granted, `false` otherwise.
139    pub const fn get_read(&self) -> bool {
140        self.get_bits(Self::READ_BIT)
141    }
142
143    /// Checks if write permission is granted.
144    ///
145    /// # Returns
146    /// `true` if write permission is granted, `false` otherwise.
147    pub const fn get_write(&self) -> bool {
148        self.get_bits(Self::WRITE_BIT)
149    }
150
151    /// Checks if execute permission is granted.
152    ///
153    /// # Returns
154    /// `true` if execute permission is granted, `false` otherwise.
155    pub const fn get_execute(&self) -> bool {
156        self.get_bits(Self::EXECUTE_BIT)
157    }
158
159    /// Converts the protection type to its raw u8 representation.
160    ///
161    /// # Returns
162    /// The raw byte value representing the protection flags.
163    pub const fn as_u8(&self) -> u8 {
164        self.0
165    }
166
167    /// Creates a new protection type from a raw u8 value.
168    ///
169    /// # Parameters
170    /// - `Value`: The raw protection flags
171    ///
172    /// # Returns
173    /// A new protection type with the specified flags.
174    pub const fn from_u8(value: u8) -> Self {
175        Self(value)
176    }
177}
178
179impl From<Protection> for u8 {
180    fn from(protection: Protection) -> Self {
181        protection.0
182    }
183}
184
185impl From<u8> for Protection {
186    fn from(protection: u8) -> Self {
187        Self(protection)
188    }
189}
190
191/// Interface for implementing memory protection operations.
192///
193/// This trait should be implemented by memory managers or other components
194/// that can apply protection settings to memory regions.
195pub trait Protectable {}