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§
Sourcefn read(&self, buffer: &mut [u8]) -> Result<Size>
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 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, buffer: &[u8]) -> Result<Size>
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 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
Sourcefn get_size(&self) -> Result<Size>
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 bytesErr(Error)
- Error if size cannot be determined
Sourcefn set_position(&self, position: &Position) -> Result<Size>
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 operationErr(Error)
- Error if position is invalid
§Errors
Error::InvalidParameter
- Position is beyond device bounds
Sourcefn flush(&self) -> Result<()>
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 successfullyErr(Error)
- Error during flush operation
Provided Methods§
Sourcefn erase(&self) -> Result<()>
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 successfullyErr(Error::UnsupportedOperation)
- Device doesn’t support eraseErr(Error)
- Error during erase operation
Sourcefn get_block_size(&self) -> Result<usize>
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 bytesErr(Error::UnsupportedOperation)
- Device doesn’t have a block size
Sourcefn is_a_terminal(&self) -> bool
fn is_a_terminal(&self) -> bool
Check if this device represents a terminal/console device.
§Returns
true
- Device is a terminalfalse
- Device is not a terminal
Sourcefn is_a_block_device(&self) -> bool
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 devicefalse
- Device is not a block device
Sourcefn dump_device(&self) -> Result<Vec<u8>>
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 contentsErr(Error)
- Error reading device
§Note
This operation can consume significant memory for large devices. Use with caution on production systems.