1#[cfg(all(feature = "async", not(feature = "std")))]
2use alloc::boxed::Box;
3
4#[cfg(feature = "async")]
5use async_trait::async_trait;
6
7use crate::error::Error;
8use crate::types::SectorID;
9
10pub type Block = [u8; 512];
11
12pub(crate) fn flatten(sector: &[Block]) -> &[u8] {
13 unsafe { core::slice::from_raw_parts(§or[0][0], sector.len() * 512) }
14}
15
16#[cfg_attr(feature = "async", async_trait)]
17#[cfg_attr(not(feature = "async"), deasync::deasync)]
18pub trait IO {
19 type Error: core::fmt::Debug;
20 fn set_sector_size_shift(&mut self, shift: u8) -> Result<(), Self::Error>;
22 async fn read<'a>(&'a mut self, id: SectorID) -> Result<&'a [Block], Self::Error>;
23 async fn write(&mut self, id: SectorID, offset: usize, data: &[u8]) -> Result<(), Self::Error>;
25 async fn flush(&mut self) -> Result<(), Self::Error>;
26}
27
28pub(crate) struct IOWrapper<IO>(IO);
29
30impl<IO> IOWrapper<IO> {
31 pub(crate) fn new(io: IO) -> Self {
32 Self(io)
33 }
34
35 pub(crate) fn unwrap(self) -> IO {
36 self.0
37 }
38}
39
40#[cfg_attr(not(feature = "async"), deasync::deasync)]
41impl<E, T: IO<Error = E>> IOWrapper<T> {
42 pub(crate) async fn read<'a>(&'a mut self, sector: SectorID) -> Result<&'a [Block], Error<E>> {
43 self.0.read(sector).await.map_err(|e| Error::IO(e))
44 }
45
46 pub(crate) async fn write(
47 &mut self,
48 id: SectorID,
49 offset: usize,
50 data: &[u8],
51 ) -> Result<(), Error<E>> {
52 let result = self.0.write(id, offset, data).await;
53 result.map_err(|e| Error::IO(e))
54 }
55
56 pub(crate) async fn flush(&mut self) -> Result<(), Error<E>> {
57 self.0.flush().await.map_err(|e| Error::IO(e))
58 }
59}
60
61#[cfg(feature = "std")]
62pub mod std;