pub trait CharDevice: Device {
// Required methods
fn read_byte(&self) -> Option<u8>;
fn write_byte(&self, byte: u8) -> Result<(), &'static str>;
fn can_read(&self) -> bool;
fn can_write(&self) -> bool;
// Provided methods
fn read(&self, buffer: &mut [u8]) -> usize { ... }
fn write(&self, buffer: &[u8]) -> Result<usize, &'static str> { ... }
fn read_at(
&self,
_position: u64,
buffer: &mut [u8],
) -> Result<usize, &'static str> { ... }
fn write_at(
&self,
_position: u64,
buffer: &[u8],
) -> Result<usize, &'static str> { ... }
fn can_seek(&self) -> bool { ... }
}Expand description
Character device interface
This trait defines the interface for character devices. It provides methods for querying device information and handling character I/O operations. Uses internal mutability for thread-safe shared access.
Required Methods§
Sourcefn read_byte(&self) -> Option<u8>
fn read_byte(&self) -> Option<u8>
Read a single byte from the device
For blocking devices (like TTY), this method will block until data is available. For non-blocking devices, this returns None if no data is available.
§Returns
The byte read from the device, or None if no data is available
Sourcefn write_byte(&self, byte: u8) -> Result<(), &'static str>
fn write_byte(&self, byte: u8) -> Result<(), &'static str>
Provided Methods§
Sourcefn read_at(
&self,
_position: u64,
buffer: &mut [u8],
) -> Result<usize, &'static str>
fn read_at( &self, _position: u64, buffer: &mut [u8], ) -> Result<usize, &'static str>
Read data from a specific position in the device
Default implementation falls back to sequential read for stream devices. Devices that support random access (like framebuffer, memory devices) should override this.
§Arguments
position- Byte offset to read frombuffer- Buffer to read data into
§Returns
Result containing the number of bytes read or an error
Sourcefn write_at(&self, _position: u64, buffer: &[u8]) -> Result<usize, &'static str>
fn write_at(&self, _position: u64, buffer: &[u8]) -> Result<usize, &'static str>
Write data to a specific position in the device
Default implementation falls back to sequential write for stream devices. Devices that support random access (like framebuffer, memory devices) should override this.
§Arguments
position- Byte offset to write tobuffer- Buffer containing data to write
§Returns
Result containing the number of bytes written or an error