Skip to main content

Disk

Trait Disk 

Source
pub trait Disk: ErrorType {
    type WRITE_GRANULARITY: ArrayLength;

    const ERASE_BLOCK_SIZE: usize;

    // Required methods
    fn read(
        &mut self,
        offset: usize,
        buf: &mut [u8],
    ) -> Result<usize, Self::Error>;
    fn write(&mut self, offset: usize, buf: &[u8]) -> Result<usize, Self::Error>;
    fn erase(&mut self, block: usize) -> Result<(), Self::Error>;
    fn block_count(&self) -> usize;

    // Provided methods
    fn read_exact(
        &mut self,
        offset: usize,
        buf: &mut [u8],
    ) -> Result<(), ReadExactError<Self::Error>> { ... }
    fn write_all(
        &mut self,
        offset: usize,
        buf: &[u8],
    ) -> Result<(), Self::Error> { ... }
}
Expand description

Represents a disk that can be read from and written to.

The disk is divided into blocks of a fixed size.

Reads and writes are in bytes but must be aligned to the disk’s write granularity.

Erases are in full block units.

The first byte of the disk is at offset 0, which is also the start of the first block with index 0.

Required Associated Constants§

Source

const ERASE_BLOCK_SIZE: usize

The size of an erasable block in bytes.

Required Associated Types§

Source

type WRITE_GRANULARITY: ArrayLength

The minimum size of a write operation in bytes.

Also defines the write alignment.

Currently, also used for read granularity.

Required Methods§

Source

fn read(&mut self, offset: usize, buf: &mut [u8]) -> Result<usize, Self::Error>

Reads data from the disk into the buffer.

The given offset is in bytes and must be aligned to WRITE_GRANULARITY. The buffer must be at least WRITE_GRANULARITY bytes long.

Source

fn write(&mut self, offset: usize, buf: &[u8]) -> Result<usize, Self::Error>

Writes data from the buffer to the disk.

The given offset is in bytes and must be aligned to WRITE_GRANULARITY. The buffer must be at least WRITE_GRANULARITY bytes long.

A write should only happen on a section of a block that has been previously erased and not yet written to. It is permissible to write independent sections of a block in any order.

Source

fn erase(&mut self, block: usize) -> Result<(), Self::Error>

Erases the block at the given block index.

The block index is in units of ERASE_BLOCK_SIZE.

The first block has index 0 and the last block has index self.block_count() - 1.

Source

fn block_count(&self) -> usize

Returns the number of blocks on the disk.

The number of blocks is fixed and does not change.

Provided Methods§

Source

fn read_exact( &mut self, offset: usize, buf: &mut [u8], ) -> Result<(), ReadExactError<Self::Error>>

Reads the exact number of bytes required to fill the buffer.

The given offset is in bytes and must be aligned to WRITE_GRANULARITY. The buffer size must be a multiple of WRITE_GRANULARITY.

If the buffer cannot be completely filled, an error is returned.

Source

fn write_all(&mut self, offset: usize, buf: &[u8]) -> Result<(), Self::Error>

Writes the exact number of bytes from the buffer to the disk.

The given offset is in bytes and must be aligned to WRITE_GRANULARITY. The buffer size must be a multiple of WRITE_GRANULARITY.

If the buffer cannot be completely written, an error is returned.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl<T: Disk> Disk for &mut T

Source§

const ERASE_BLOCK_SIZE: usize = T::ERASE_BLOCK_SIZE

Source§

type WRITE_GRANULARITY = <T as Disk>::WRITE_GRANULARITY

Source§

fn read(&mut self, offset: usize, buf: &mut [u8]) -> Result<usize, Self::Error>

Source§

fn write(&mut self, offset: usize, buf: &[u8]) -> Result<usize, Self::Error>

Source§

fn erase(&mut self, block: usize) -> Result<(), Self::Error>

Source§

fn block_count(&self) -> usize

Implementors§