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(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(&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>
impl<const BLOCK_SIZE: u32> MemoryDevice<BLOCK_SIZE>
Sourcepub fn new(size: usize) -> Self
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);pub fn new_static(size: usize) -> &'static Self
Sourcepub fn from_vec(data: Vec<u8>) -> Self
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>
impl<const BLOCK_SIZE: u32> Debug for MemoryDevice<BLOCK_SIZE>
Source§impl<const BLOCK_SIZE: u32> DirectBaseOperations for MemoryDevice<BLOCK_SIZE>
impl<const BLOCK_SIZE: u32> DirectBaseOperations for MemoryDevice<BLOCK_SIZE>
Source§fn read(&self, buffer: &mut [u8], absolute_position: Size) -> Result<usize>
fn read(&self, buffer: &mut [u8], absolute_position: Size) -> 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.