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§
Sourcefn read(
&self,
context: &mut Context,
buffer: &mut [u8],
absolute_position: Size,
) -> Result<usize>
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 contextBuffer- Mutable byte slice to read data into
§Returns
Ok(Size)- Number of bytes successfully readErr(Error)- Error if read operation failed
§Errors
Error::InputOutput- I/O error during read operationError::RessourceBusy- Device is temporarily unavailableError::InvalidParameter- Invalid buffer or device state
Sourcefn write(
&self,
context: &mut Context,
buffer: &[u8],
absolute_position: Size,
) -> Result<usize>
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 contextBuffer- Byte slice containing data to write
§Returns
Ok(Size)- Number of bytes successfully writtenErr(Error)- Error if write operation failed
§Errors
Error::InputOutput- I/O error during write operationError::NoSpaceLeft- Device is fullError::RessourceBusy- Device is temporarily unavailableError::PermissionDenied- Device is read-only
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>
Sourcefn set_position(
&self,
_context: &mut Context,
_current_position: Size,
_position: &Position,
) -> Result<Size>
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 contextPosition- The new position to set
§Returns
Ok(Size)- The new absolute position after the operationErr(Error)- Error if position is invalid
§Errors
Error::InvalidParameter- Position is beyond device bounds
Sourcefn flush(&self, _context: &mut Context) -> Result<()>
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 successfullyErr(Error)- Error during flush operation