#![deny(unconditional_recursion)]
#![no_std]
use generic_array::ArrayLength;
pub trait Disk: embedded_io::ErrorType {
const ERASE_BLOCK_SIZE: usize;
#[allow(non_camel_case_types)]
type WRITE_GRANULARITY: ArrayLength;
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;
#[track_caller]
fn read_exact(
&mut self,
mut offset: usize,
mut buf: &mut [u8],
) -> Result<(), embedded_io::ReadExactError<Self::Error>> {
while !buf.is_empty() {
let bytes_read = self.read(offset, buf)?;
if bytes_read == 0 {
return Err(embedded_io::ReadExactError::UnexpectedEof);
}
offset += bytes_read;
buf = &mut buf[bytes_read..];
}
Ok(())
}
#[track_caller]
fn write_all(&mut self, mut offset: usize, mut buf: &[u8]) -> Result<(), Self::Error> {
while !buf.is_empty() {
let bytes_written = self.write(offset, buf)?;
if bytes_written == 0 {
panic!("write() returned Ok(0)");
}
offset += bytes_written;
buf = &buf[bytes_written..];
}
Ok(())
}
}
impl<T: Disk> Disk for &mut T {
type WRITE_GRANULARITY = T::WRITE_GRANULARITY;
const ERASE_BLOCK_SIZE: usize = T::ERASE_BLOCK_SIZE;
#[track_caller]
fn read(&mut self, offset: usize, buf: &mut [u8]) -> Result<usize, Self::Error> {
(**self).read(offset, buf)
}
#[track_caller]
fn write(&mut self, offset: usize, buf: &[u8]) -> Result<usize, Self::Error> {
(**self).write(offset, buf)
}
#[track_caller]
fn erase(&mut self, block: usize) -> Result<(), Self::Error> {
(**self).erase(block)
}
#[track_caller]
fn block_count(&self) -> usize {
(**self).block_count()
}
}