Skip to main content

MemoryDevice

Struct MemoryDevice 

Source
pub struct MemoryDevice<const BLOCK_SIZE: u32>(/* 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

extern crate alloc;
use file_system::{MemoryDevice, DirectBaseOperations, Position};

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

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

// Reset position and read back
device.set_position(0, &Position::Start(0)).unwrap();
let mut buffer = alloc::vec![0u8; data.len()];
device.read_sync(&mut buffer, 0).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: u32> 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 = MemoryDevice::<512>::new(4096);
Source

pub fn new_static(size: usize) -> &'static Self

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 = MemoryDevice::<512>::from_vec(data);

Trait Implementations§

Source§

impl<const BLOCK_SIZE: u32> Debug for MemoryDevice<BLOCK_SIZE>

Source§

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

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

impl<const BLOCK_SIZE: u32> DirectBaseOperations for MemoryDevice<BLOCK_SIZE>

Source§

fn read( &self, _cx: &mut Context<'_>, buffer: &mut [u8], absolute_position: Size, ) -> Poll<Result<usize>>

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, _cx: &mut Context<'_>, buffer: &[u8], absolute_position: Size, ) -> Poll<Result<usize>>

Source§

fn control( &self, _cx: &mut Context<'_>, command: ControlCommandIdentifier, _: &AnyByLayout, output: &mut AnyByLayout, ) -> Poll<Result<()>>

Source§

fn flush(&self, _cx: &mut TaskContext<'_>) -> Poll<Result<()>>

Source§

fn read_until( &self, cx: &mut TaskContext<'_>, buffer: &mut [u8], absolute_position: Size, delimiter: &[u8], ) -> Poll<Result<usize>>

Source§

fn write_pattern( &self, cx: &mut TaskContext<'_>, pattern: &[u8], count: usize, absolute_position: Size, ) -> Poll<Result<usize>>

Source§

fn write_vectored( &self, cx: &mut TaskContext<'_>, buffers: &[&[u8]], absolute_position: Size, ) -> Poll<Result<usize>>

Source§

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

Source§

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

Source§

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

Source§

fn control_sync( &self, command: ControlCommandIdentifier, input: &AnyByLayout, output: &mut AnyByLayout, ) -> Result<()>

Source§

fn read_until_sync( &self, buffer: &mut [u8], absolute_position: Size, delimiter: &[u8], ) -> Result<usize>

Source§

fn write_pattern_sync( &self, pattern: &[u8], count: usize, absolute_position: Size, ) -> Result<usize>

Source§

fn write_vectored_sync( &self, buffers: &[&[u8]], absolute_position: Size, ) -> Result<usize>

Source§

fn cancel_operation(&self, _operation: Operation)

Detach the current operation’s registration; see BaseOperations.
Source§

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

Source§

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

Source§

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

Source§

impl<const BLOCK_SIZE: u32> DirectBlockDevice for MemoryDevice<BLOCK_SIZE>

Source§

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

Source§

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

Source§

fn erase(&self, position: Size, size: Size) -> Result<()>

Prepare a byte range for writing, as raw flash requires before programming. Read more
Source§

impl<const BLOCK_SIZE: u32> MountOperations for MemoryDevice<BLOCK_SIZE>

Source§

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

Source§

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

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

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

§

impl<const BLOCK_SIZE: u32> UnsafeUnpin for MemoryDevice<BLOCK_SIZE>

§

impl<const BLOCK_SIZE: u32> 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> BaseOperations for T

Source§

fn read( &self, _: &mut Context, cx: &mut Context<'_>, buffer: &mut [u8], absolute_position: u64, ) -> Poll<Result<usize, Kind>>

Source§

fn write( &self, _: &mut Context, cx: &mut Context<'_>, buffer: &[u8], absolute_position: u64, ) -> Poll<Result<usize, Kind>>

Source§

fn flush(&self, _: &mut Context, cx: &mut Context<'_>) -> Poll<Result<(), Kind>>

Source§

fn control( &self, _: &mut Context, cx: &mut Context<'_>, command: ControlCommandIdentifier, input: &AnyByLayout, output: &mut AnyByLayout, ) -> Poll<Result<(), Kind>>

Source§

fn read_until( &self, _: &mut Context, cx: &mut Context<'_>, buffer: &mut [u8], absolute_position: u64, delimiter: &[u8], ) -> Poll<Result<usize, Kind>>

Source§

fn write_pattern( &self, _: &mut Context, cx: &mut Context<'_>, pattern: &[u8], count: usize, absolute_position: u64, ) -> Poll<Result<usize, Kind>>

Source§

fn write_vectored( &self, _: &mut Context, cx: &mut Context<'_>, buffers: &[&[u8]], absolute_position: u64, ) -> Poll<Result<usize, Kind>>

Source§

fn open(&self, _: &mut Context) -> Result<(), Kind>

Source§

fn close(&self, _: &mut Context) -> Result<(), Kind>

Source§

fn set_position( &self, _: &mut Context, current: u64, position: &Position, ) -> Result<u64, Kind>

Source§

fn clone_context(&self, _: &Context) -> Result<Context, Kind>

Source§

fn cancel_operation(&self, _: &mut Context, operation: Operation)

Detach this opening’s registration after completion, error or cancellation. This hook is idempotent and must not consume data or change the cursor. Wake and drop retired wakers outside device locks.
Source§

fn read_sync( &self, context: &mut Context, buffer: &mut [u8], absolute_position: Size, ) -> Result<usize>

Source§

fn write_sync( &self, context: &mut Context, buffer: &[u8], absolute_position: Size, ) -> Result<usize>

Source§

fn flush_sync(&self, context: &mut Context) -> Result<()>

Source§

fn control_sync( &self, context: &mut Context, command: ControlCommandIdentifier, input: &AnyByLayout, output: &mut AnyByLayout, ) -> Result<()>

Source§

fn read_until_sync( &self, context: &mut Context, buffer: &mut [u8], absolute_position: Size, delimiter: &[u8], ) -> Result<usize>

Source§

fn write_pattern_sync( &self, context: &mut Context, pattern: &[u8], count: usize, absolute_position: Size, ) -> Result<usize>

Source§

fn write_vectored_sync( &self, context: &mut Context, buffers: &[&[u8]], absolute_position: Size, ) -> Result<usize>

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.
Source§

impl<T> BlockDevice for T