1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
use core::pin::Pin;
use core::task::{Context, Poll};

/// Trait that must be implemented by the serial port that enables async communication
pub trait AsyncReadWrite {
    /// Device error
    #[cfg(feature = "std")]
    type Error: snafu::AsErrorSource;
    #[cfg(not(feature = "std"))]
    type Error;

    /// Attempt to read from the object into `buf`.
    fn poll_read(
        self: Pin<&mut Self>,
        cx: &mut Context<'_>,
        buf: &mut [u8],
    ) -> Poll<Result<usize, Self::Error>>;

    /// Attempt to write bytes from `buf` into the object.
    fn poll_write(
        self: Pin<&mut Self>,
        cx: &mut Context<'_>,
        buf: &[u8],
    ) -> Poll<Result<usize, Self::Error>>;
}

#[cfg(feature = "std")]
impl<T> AsyncReadWrite for T
where
    T: futures::io::AsyncRead + futures::io::AsyncWrite + Unpin,
{
    type Error = futures::io::Error;

    fn poll_read(
        self: Pin<&mut Self>,
        cx: &mut Context<'_>,
        buf: &mut [u8],
    ) -> Poll<Result<usize, Self::Error>> {
        futures::io::AsyncRead::poll_read(self, cx, buf)
    }

    fn poll_write(
        self: Pin<&mut Self>,
        cx: &mut Context<'_>,
        buf: &[u8],
    ) -> Poll<Result<usize, Self::Error>> {
        futures::io::AsyncWrite::poll_write(self, cx, buf)
    }
}