Struct MemSafe

Source
pub struct MemSafe<T> { /* private fields */ }
Expand description

MemSafe allows for a protected memory space with controlled access to prevent unauthorized access and ensure memory safety.

§Examples

use memsafe::MemSafe;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut safe_data = MemSafe::new(42)?;

    // Read access
    {
        let reader = safe_data.read()?;
        assert_eq!(*reader, 42);
    } // reader is dropped, privileges are released

    // Write access
    {
        let mut writer = safe_data.write()?;
        *writer = 100;
    } // writer is dropped, privileges are released

    // Verify the change
    {
        let reader = safe_data.read()?;
        assert_eq!(*reader, 100);
    }
}

Implementations§

Source§

impl<T> MemSafe<T>

Source

pub fn new(value: T) -> Result<MemSafe<T>, MemoryError>

Initialize a protected memory region containing the specified value, with lowest possible memory access controls applied.

Lowest access level:

PlatformReadWrite
Unix
Windows
§Errors

Returns a MemoryError if memory protection could not be initialized.

§Examples
use memsafe::MemSafe;

let safe_data = MemSafe::new([0_u8; 32]).unwrap();
Source

pub fn read(&mut self) -> Result<MemSafeRead<'_, T>, MemoryError>

Obtains read-only access to the protected memory region. This method temporarily elevates the read privileges and returns a handle that implements Deref for accessing the inner value. When the returned MemSafeRead is dropped, privileges are automatically revoked on Unix-based OSes.

§Errors

Returns a MemoryError if privilege elevation fails.

Source

pub fn write(&mut self) -> Result<MemSafeWrite<'_, T>, MemoryError>

Obtains mutable access to the protected memory region. This method temporarily elevates the read and write privileges and returns a handle that implements Deref and DerefMutfor modifying the inner value. When the returned MemSafeWrite is dropped, privileges are automatically revoked on Unix-based OSes. On Windows read, privileges are maintained while write privileges are revoked.

§Errors

Returns a MemoryError if privilege elevation fails.

Trait Implementations§

Source§

impl<T: Debug> Debug for MemSafe<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T> Send for MemSafe<T>
where T: Send,

Auto Trait Implementations§

§

impl<T> Freeze for MemSafe<T>

§

impl<T> RefUnwindSafe for MemSafe<T>
where T: RefUnwindSafe,

§

impl<T> !Sync for MemSafe<T>

§

impl<T> Unpin for MemSafe<T>

§

impl<T> UnwindSafe for MemSafe<T>
where T: RefUnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.