Struct MemoryDevice

Source
pub struct MemoryDevice<const BLOCK_SIZE: usize>(/* private fields */);
Expand description

In-memory device implementation with configurable block size.

This device stores all data in memory using a Vec<u8> and provides the same interface as physical storage devices. It’s thread-safe and supports all standard device operations. The block size is configurable at compile time through the const generic parameter.

§Type Parameters

  • Block_size - The block size in bytes (must be a power of 2, typically 512)

§Examples


// Create a 1MB memory device with 512-byte blocks
let device = Memory_device_type::<512>::new(1024 * 1024);
let device = create_device!(device);

// Write some data
let data = b"Hello, Memory Device!";
device.Write(data).unwrap();

// Reset position and read back
device.Set_position(&Position_type::Start(0)).unwrap();
let mut buffer = alloc::vec![0u8; data.len()];
device.Read(&mut buffer).unwrap();
assert_eq!(&buffer, data);

§Thread Safety

The device uses an RwLock to ensure thread-safe access to the underlying data. Multiple readers can access the device simultaneously, but writes are exclusive.

Implementations§

Source§

impl<const BLOCK_SIZE: usize> MemoryDevice<BLOCK_SIZE>

Source

pub fn new(size: usize) -> Self

Create a new memory device with the specified size.

The device will be initialized with zeros and have the specified total size. The size must be a multiple of the block size.

§Arguments
  • Size - Total size of the device in bytes
§Panics

Panics if Size is not a multiple of Block_size.

§Examples

// Create a 4KB device with 512-byte blocks
let device = Memory_device_type::<512>::new(4096);
Source

pub fn from_vec(data: Vec<u8>) -> Self

Create a memory device from existing data.

This allows you to create a device with pre-populated data, useful for testing with known data patterns or loading device images.

§Arguments
  • Data - Vector containing the initial device data
§Panics

Panics if the data length is not a multiple of Block_size.

§Examples

// Create device with specific data
let data = vec![0x42; 1024]; // 1KB of 0x42 bytes
let device = Memory_device_type::<512>::From_vec(data);
Source

pub fn get_block_count(&self) -> usize

Get the number of blocks in this device.

Returns the total number of blocks of size Block_size that fit in the device.

§Returns

The number of blocks in the device.

§Examples

let device = Memory_device_type::<512>::new(2048);
assert_eq!(device.get_block_count(), 4); // 2048 / 512 = 4

Trait Implementations§

Source§

impl<const BLOCK_SIZE: usize> DeviceTrait for MemoryDevice<BLOCK_SIZE>

Source§

fn read(&self, buffer: &mut [u8]) -> Result<Size>

Read data from the memory device.

Reads data from the current position into the provided buffer. The position is automatically advanced by the number of bytes read.

Source§

fn write(&self, buffer: &[u8]) -> Result<Size>

Write data to the device at the current position. Read more
Source§

fn get_size(&self) -> Result<Size>

Get the total size of the device in bytes. Read more
Source§

fn set_position(&self, position: &Position) -> Result<Size>

Set the current position cursor for read/write operations. Read more
Source§

fn erase(&self) -> Result<()>

Erase the entire device. Read more
Source§

fn flush(&self) -> Result<()>

Flush any buffered data to the underlying storage. Read more
Source§

fn get_block_size(&self) -> Result<usize>

Get the block size of the device in bytes. Read more
Source§

fn dump_device(&self) -> Result<Vec<u8>>

Create a complete dump of the device contents. Read more
Source§

fn is_a_terminal(&self) -> bool

Check if this device represents a terminal/console device. Read more
Source§

fn is_a_block_device(&self) -> bool

Check if this device is a block device. Read more

Auto Trait Implementations§

§

impl<const BLOCK_SIZE: usize> !Freeze for MemoryDevice<BLOCK_SIZE>

§

impl<const BLOCK_SIZE: usize> !RefUnwindSafe for MemoryDevice<BLOCK_SIZE>

§

impl<const BLOCK_SIZE: usize> Send for MemoryDevice<BLOCK_SIZE>

§

impl<const BLOCK_SIZE: usize> Sync for MemoryDevice<BLOCK_SIZE>

§

impl<const BLOCK_SIZE: usize> Unpin for MemoryDevice<BLOCK_SIZE>

§

impl<const BLOCK_SIZE: usize> UnwindSafe for MemoryDevice<BLOCK_SIZE>

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.