1use crate::error::{Error, Result};
16
17pub trait BlockRead: Send + Sync {
19 fn read_at(&self, offset: u64, buf: &mut [u8]) -> Result<()>;
22
23 fn size_bytes(&self) -> u64;
25}
26
27pub trait BlockDevice: BlockRead {
31 fn write_at(&self, _offset: u64, _buf: &[u8]) -> Result<()> {
34 Err(Error::ReadOnly)
35 }
36
37 fn flush(&self) -> Result<()> {
39 Ok(())
40 }
41
42 fn is_writable(&self) -> bool {
45 false
46 }
47}
48
49impl<T: BlockRead + ?Sized> BlockRead for std::sync::Arc<T> {
53 fn read_at(&self, offset: u64, buf: &mut [u8]) -> Result<()> {
54 (**self).read_at(offset, buf)
55 }
56 fn size_bytes(&self) -> u64 {
57 (**self).size_bytes()
58 }
59}
60
61impl<T: BlockDevice + ?Sized> BlockDevice for std::sync::Arc<T> {
62 fn write_at(&self, offset: u64, buf: &[u8]) -> Result<()> {
63 (**self).write_at(offset, buf)
64 }
65 fn flush(&self) -> Result<()> {
66 (**self).flush()
67 }
68 fn is_writable(&self) -> bool {
69 (**self).is_writable()
70 }
71}
72
73impl<T: BlockRead + ?Sized> BlockRead for Box<T> {
74 fn read_at(&self, offset: u64, buf: &mut [u8]) -> Result<()> {
75 (**self).read_at(offset, buf)
76 }
77 fn size_bytes(&self) -> u64 {
78 (**self).size_bytes()
79 }
80}
81
82impl<T: BlockRead + ?Sized> BlockRead for &T {
83 fn read_at(&self, offset: u64, buf: &mut [u8]) -> Result<()> {
84 (**self).read_at(offset, buf)
85 }
86 fn size_bytes(&self) -> u64 {
87 (**self).size_bytes()
88 }
89}
90
91impl<T: BlockDevice + ?Sized> BlockDevice for Box<T> {
92 fn write_at(&self, offset: u64, buf: &[u8]) -> Result<()> {
93 (**self).write_at(offset, buf)
94 }
95 fn flush(&self) -> Result<()> {
96 (**self).flush()
97 }
98 fn is_writable(&self) -> bool {
99 (**self).is_writable()
100 }
101}