CharDevice

Trait CharDevice 

Source
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§

Source

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

Source

fn write_byte(&self, byte: u8) -> Result<(), &'static str>

Write a single byte to the device

§Arguments
  • byte - The byte to write to the device
§Returns

Result indicating success or failure

Source

fn can_read(&self) -> bool

Check if the device is ready for reading

Source

fn can_write(&self) -> bool

Check if the device is ready for writing

Provided Methods§

Source

fn read(&self, buffer: &mut [u8]) -> usize

Read multiple bytes from the device

§Arguments
  • buffer - The buffer to read data into
§Returns

The number of bytes actually read

Source

fn write(&self, buffer: &[u8]) -> Result<usize, &'static str>

Write multiple bytes to the device

§Arguments
  • buffer - The buffer containing data to write
§Returns

Result containing the number of bytes written or an error

Source

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 from
  • buffer - Buffer to read data into
§Returns

Result containing the number of bytes read or an error

Source

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 to
  • buffer - Buffer containing data to write
§Returns

Result containing the number of bytes written or an error

Source

fn can_seek(&self) -> bool

Check if this device supports seek operations

Default implementation returns false for stream devices. Devices that support seeking should override this.

§Returns

True if the device supports seek operations

Implementors§