BaseOperations

Trait BaseOperations 

Source
pub trait BaseOperations: Send + Sync {
    // Required methods
    fn read(
        &self,
        context: &mut Context,
        buffer: &mut [u8],
        absolute_position: Size,
    ) -> Result<usize>;
    fn write(
        &self,
        context: &mut Context,
        buffer: &[u8],
        absolute_position: Size,
    ) -> Result<usize>;
    fn clone_context(&self, context: &Context) -> Result<Context>;

    // Provided methods
    fn open(&self, _context: &mut Context) -> Result<()> { ... }
    fn close(&self, _context: &mut Context) -> Result<()> { ... }
    fn read_until(
        &self,
        context: &mut Context,
        buffer: &mut [u8],
        absolute_position: Size,
        delimiter: &[u8],
    ) -> Result<usize> { ... }
    fn write_pattern(
        &self,
        context: &mut Context,
        pattern: &[u8],
        count: usize,
        absolute_position: Size,
    ) -> Result<usize> { ... }
    fn write_vectored(
        &self,
        context: &mut Context,
        buffers: &[&[u8]],
        absolute_position: Size,
    ) -> Result<usize> { ... }
    fn set_position(
        &self,
        _context: &mut Context,
        _current_position: Size,
        _position: &Position,
    ) -> Result<Size> { ... }
    fn flush(&self, _context: &mut Context) -> Result<()> { ... }
    fn control(
        &self,
        _context: &mut Context,
        _command: ControlCommandIdentifier,
        _input: &AnyByLayout,
        _output: &mut AnyByLayout,
    ) -> Result<()> { ... }
}
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 BaseOperations::set_position.

§Examples


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

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

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

Required Methods§

Source

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

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
  • context - File system context
  • 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, context: &mut Context, buffer: &[u8], absolute_position: Size, ) -> Result<usize>

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
  • context - File system context
  • 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 clone_context(&self, context: &Context) -> Result<Context>

Provided Methods§

Source

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

Source

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

Source

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

Source

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

Source

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

Source

fn set_position( &self, _context: &mut Context, _current_position: Size, _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
  • context - File system context
  • 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, _context: &mut Context) -> 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.

§Arguments
  • context - File system context
§Returns
  • Ok(()) - Flush completed successfully
  • Err(Error) - Error during flush operation
Source

fn control( &self, _context: &mut Context, _command: ControlCommandIdentifier, _input: &AnyByLayout, _output: &mut AnyByLayout, ) -> Result<()>

Implementors§