Trait DeviceTrait

Source
pub trait DeviceTrait: Send + Sync {
    // Required methods
    fn read(&self, buffer: &mut [u8]) -> Result<Size>;
    fn write(&self, buffer: &[u8]) -> Result<Size>;
    fn get_size(&self) -> Result<Size>;
    fn set_position(&self, position: &Position) -> Result<Size>;
    fn flush(&self) -> Result<()>;

    // Provided methods
    fn erase(&self) -> Result<()> { ... }
    fn get_block_size(&self) -> Result<usize> { ... }
    fn is_a_terminal(&self) -> bool { ... }
    fn is_a_block_device(&self) -> bool { ... }
    fn dump_device(&self) -> Result<Vec<u8>> { ... }
}
Expand description

Core trait for all device implementations in the file system.

A device represents any storage medium or I/O endpoint that can be read from and written to. This includes physical storage devices (hard drives, SSDs, SD cards), memory devices for testing, partition devices, and other specialized I/O devices.

§Thread Safety

All device implementations must be thread-safe (Send + Sync) as they may be accessed by multiple tasks/threads concurrently. Implementations should use appropriate synchronization primitives like RwLock or Mutex to handle concurrent access.

§Non-Blocking Operations

Devices should never block indefinitely. If an operation would block, implementations should return Error::RessourceBusy instead. This means implementations should prefer try_read() and try_write() variants of synchronization primitives.

§Position Management

Devices maintain an internal position cursor that affects read and write operations. The position can be manipulated using DeviceTrait::set_position.

§Examples


// Create a memory device for testing
let device = create_device!(Memory_device_type::<512>::new(1024));

// Write data
let data = b"Hello, World!";
let bytes_written = device.Write(data).unwrap();
assert_eq!(bytes_written.As_u64(), data.len() as u64);

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

Required Methods§

Source

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

Read data from the device at the current position.

Reads up to Buffer.len() bytes from the device into the provided buffer. The actual number of bytes read may be less than requested.

§Arguments
  • Buffer - Mutable byte slice to read data into
§Returns
  • Ok(Size) - Number of bytes successfully read
  • Err(Error) - Error if read operation failed
§Errors
Source

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

Write data to the device at the current position.

Writes up to Buffer.len() bytes from the buffer to the device. The actual number of bytes written may be less than requested.

§Arguments
  • Buffer - Byte slice containing data to write
§Returns
  • Ok(Size) - Number of bytes successfully written
  • Err(Error) - Error if write operation failed
§Errors
Source

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

Get the total size of the device in bytes.

Returns the maximum amount of data that can be stored on or read from the device.

§Returns
  • Ok(Size) - Total device size in bytes
  • Err(Error) - Error if size cannot be determined
Source

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

Set the current position cursor for read/write operations.

The position affects where subsequent read and write operations will occur. Different position types allow for absolute positioning, relative positioning, and positioning from the end of the device.

§Arguments
  • Position - The new position to set
§Returns
  • Ok(Size) - The new absolute position after the operation
  • Err(Error) - Error if position is invalid
§Errors
Source

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

Flush any buffered data to the underlying storage.

Ensures that all pending write operations are committed to the physical device. This is important for data integrity, especially on buffered devices.

§Returns
  • Ok(()) - Flush completed successfully
  • Err(Error) - Error during flush operation

Provided Methods§

Source

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

Erase the entire device.

This operation is primarily intended for flash memory devices and similar storage that requires explicit erase operations. For most devices, this operation is not supported and will return Error::UnsupportedOperation.

§Returns
  • Ok(()) - Erase completed successfully
  • Err(Error::UnsupportedOperation) - Device doesn’t support erase
  • Err(Error) - Error during erase operation
Source

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

Get the block size of the device in bytes.

For block devices, this returns the minimum unit of data transfer. Operations should ideally be aligned to block boundaries for optimal performance.

§Returns
  • Ok(usize) - Block size in bytes
  • Err(Error::UnsupportedOperation) - Device doesn’t have a block size
Source

fn is_a_terminal(&self) -> bool

Check if this device represents a terminal/console device.

§Returns
  • true - Device is a terminal
  • false - Device is not a terminal
Source

fn is_a_block_device(&self) -> bool

Check if this device is a block device.

Block devices support fixed-size block operations and typically represent physical storage media like hard drives or SSDs.

§Returns
  • true - Device is a block device
  • false - Device is not a block device
Source

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

Create a complete dump of the device contents.

Reads the entire device contents into a vector. This is primarily useful for debugging, testing, and creating backups of small devices.

§Returns
  • Ok(Vec<u8>) - Complete device contents
  • Err(Error) - Error reading device
§Note

This operation can consume significant memory for large devices. Use with caution on production systems.

Implementors§

Source§

impl DeviceTrait for PartitionDevice

Source§

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