use crate::error::{Error, Result};
pub trait BlockRead: Send + Sync {
fn read_at(&self, offset: u64, buf: &mut [u8]) -> Result<()>;
fn size_bytes(&self) -> u64;
}
pub trait BlockDevice: BlockRead {
fn write_at(&self, _offset: u64, _buf: &[u8]) -> Result<()> {
Err(Error::ReadOnly)
}
fn flush(&self) -> Result<()> {
Ok(())
}
fn is_writable(&self) -> bool {
false
}
}
impl<T: BlockRead + ?Sized> BlockRead for std::sync::Arc<T> {
fn read_at(&self, offset: u64, buf: &mut [u8]) -> Result<()> {
(**self).read_at(offset, buf)
}
fn size_bytes(&self) -> u64 {
(**self).size_bytes()
}
}
impl<T: BlockDevice + ?Sized> BlockDevice for std::sync::Arc<T> {
fn write_at(&self, offset: u64, buf: &[u8]) -> Result<()> {
(**self).write_at(offset, buf)
}
fn flush(&self) -> Result<()> {
(**self).flush()
}
fn is_writable(&self) -> bool {
(**self).is_writable()
}
}
impl<T: BlockRead + ?Sized> BlockRead for Box<T> {
fn read_at(&self, offset: u64, buf: &mut [u8]) -> Result<()> {
(**self).read_at(offset, buf)
}
fn size_bytes(&self) -> u64 {
(**self).size_bytes()
}
}
impl<T: BlockRead + ?Sized> BlockRead for &T {
fn read_at(&self, offset: u64, buf: &mut [u8]) -> Result<()> {
(**self).read_at(offset, buf)
}
fn size_bytes(&self) -> u64 {
(**self).size_bytes()
}
}
impl<T: BlockDevice + ?Sized> BlockDevice for Box<T> {
fn write_at(&self, offset: u64, buf: &[u8]) -> Result<()> {
(**self).write_at(offset, buf)
}
fn flush(&self) -> Result<()> {
(**self).flush()
}
fn is_writable(&self) -> bool {
(**self).is_writable()
}
}