mavio/io/adapters/
embedded_io_async.rs1use embedded_io_async::ReadExactError;
2
3use crate::error::{IoError, IoErrorKind};
4use crate::io::{AsyncRead, AsyncWrite};
5
6pub struct EmbeddedIoAsyncReader<R: embedded_io_async::Read> {
8 reader: R,
9}
10
11impl<R: embedded_io_async::Read> EmbeddedIoAsyncReader<R> {
12 #[inline(always)]
14 pub fn new(reader: R) -> Self {
15 Self { reader }
16 }
17
18 #[inline(always)]
20 pub fn extract(self) -> R {
21 self.reader
22 }
23}
24
25impl<R: embedded_io_async::Read> From<R> for EmbeddedIoAsyncReader<R> {
26 fn from(value: R) -> Self {
27 Self::new(value)
28 }
29}
30
31impl<R: embedded_io_async::Read> AsyncRead<IoError> for EmbeddedIoAsyncReader<R> {
32 #[inline(always)]
33 async fn read_exact<'a>(&'a mut self, buf: &'a mut [u8]) -> Result<(), IoError> {
34 self.reader.read_exact(buf).await.map_err(|err| match err {
35 ReadExactError::UnexpectedEof => IoError::from(IoErrorKind::UnexpectedEof),
36 ReadExactError::Other(err) => IoError::from_embedded_io_error(err),
37 })
38 }
39}
40
41pub struct EmbeddedIoAsyncWriter<W: embedded_io_async::Write> {
43 writer: W,
44}
45
46impl<W: embedded_io_async::Write> EmbeddedIoAsyncWriter<W> {
47 #[inline(always)]
49 pub fn new(writer: W) -> Self {
50 Self { writer }
51 }
52
53 #[inline(always)]
55 pub fn extract(self) -> W {
56 self.writer
57 }
58}
59
60impl<W: embedded_io_async::Write> AsyncWrite<IoError> for EmbeddedIoAsyncWriter<W> {
61 #[inline(always)]
62 async fn write_all<'a>(&'a mut self, buf: &'a [u8]) -> Result<(), IoError> {
63 self.writer
64 .write_all(buf)
65 .await
66 .map_err(IoError::from_embedded_io_error)
67 }
68
69 #[inline]
70 async fn flush(&mut self) -> Result<(), IoError> {
71 self.writer
72 .flush()
73 .await
74 .map_err(IoError::from_embedded_io_error)
75 }
76}