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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
//
// Copyright 2024, Colias Group, LLC
// Copyright 2024, Embedded devices Working Group
//
// SPDX-License-Identifier: MIT OR Apache-2.0
//

// TODO use Pin

#![no_std]

use core::future::{poll_fn, Future};
use core::pin::{pin, Pin};
use core::task::{Context, Poll};

use embedded_io_async as eio;

pub use embedded_io_async::{Error, ErrorKind, ErrorType};

pub trait Read: ErrorType {
    fn poll_read(
        self: Pin<&mut Self>,
        cx: &mut Context<'_>,
        buf: &mut [u8],
    ) -> Poll<Result<usize, Self::Error>>;
}

pub trait Write: ErrorType {
    fn poll_write(
        self: Pin<&mut Self>,
        cx: &mut Context<'_>,
        buf: &[u8],
    ) -> Poll<Result<usize, Self::Error>>;

    fn poll_flush(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
        Poll::Ready(Ok(()))
    }
}

impl<T: Read + Unpin> Read for &mut T {
    fn poll_read(
        self: Pin<&mut Self>,
        cx: &mut Context<'_>,
        buf: &mut [u8],
    ) -> Poll<Result<usize, Self::Error>> {
        T::poll_read(Pin::new(*Pin::into_inner(self)), cx, buf)
    }
}

impl<T: Write + Unpin> Write for &mut T {
    fn poll_write(
        self: Pin<&mut Self>,
        cx: &mut Context<'_>,
        buf: &[u8],
    ) -> Poll<Result<usize, Self::Error>> {
        T::poll_write(Pin::new(*Pin::into_inner(self)), cx, buf)
    }

    fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
        T::poll_flush(Pin::new(*Pin::into_inner(self)), cx)
    }
}

#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct EmbeddedIOAsyncAdapter<T>(pub T);

impl<T: ErrorType> ErrorType for EmbeddedIOAsyncAdapter<T> {
    type Error = T::Error;
}

impl<T> eio::Read for EmbeddedIOAsyncAdapter<T>
where
    T: Read + Unpin,
{
    async fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> {
        poll_fn(|cx| Pin::new(&mut self.0).poll_read(cx, buf)).await
    }
}

impl<T> eio::Write for EmbeddedIOAsyncAdapter<T>
where
    T: Write + Unpin,
{
    async fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> {
        poll_fn(|cx| Pin::new(&mut self.0).poll_write(cx, buf)).await
    }

    async fn flush(&mut self) -> Result<(), Self::Error> {
        poll_fn(|cx| Pin::new(&mut self.0).poll_flush(cx)).await
    }
}

pub trait ReadCancelSafe: eio::Read {}
pub trait FlushCancelSafe: eio::Write {}
pub trait WriteCancelSafe: FlushCancelSafe {}

impl<T: Read + Unpin> ReadCancelSafe for EmbeddedIOAsyncAdapter<T> {}
impl<T: Write + Unpin> FlushCancelSafe for EmbeddedIOAsyncAdapter<T> {}
impl<T: Write + Unpin> WriteCancelSafe for EmbeddedIOAsyncAdapter<T> {}

impl<T> Read for EmbeddedIOAsyncAdapter<T>
where
    T: eio::Read + ReadCancelSafe + Unpin,
{
    fn poll_read(
        mut self: Pin<&mut Self>,
        cx: &mut Context<'_>,
        buf: &mut [u8],
    ) -> Poll<Result<usize, Self::Error>> {
        pin!(self.0.read(buf)).poll(cx)
    }
}

impl<T> Write for EmbeddedIOAsyncAdapter<T>
where
    T: eio::Write + WriteCancelSafe + Unpin,
{
    fn poll_write(
        mut self: Pin<&mut Self>,
        cx: &mut Context<'_>,
        buf: &[u8],
    ) -> Poll<Result<usize, Self::Error>> {
        pin!(self.0.write(buf)).poll(cx)
    }

    fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
        pin!(self.0.flush()).poll(cx)
    }
}

#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct EmbeddedIOAsyncAdapterUsingReady<T>(pub T);

impl<T: ErrorType> ErrorType for EmbeddedIOAsyncAdapterUsingReady<T> {
    type Error = T::Error;
}

impl<T> Read for EmbeddedIOAsyncAdapterUsingReady<T>
where
    T: eio::Read + eio::ReadReady + Unpin,
{
    fn poll_read(
        mut self: Pin<&mut Self>,
        cx: &mut Context<'_>,
        buf: &mut [u8],
    ) -> Poll<Result<usize, Self::Error>> {
        if self.0.read_ready()? {
            match pin!(self.0.read(buf)).poll(cx) {
                Poll::Ready(r) => Poll::Ready(r),
                Poll::Pending => unreachable!(),
            }
        } else {
            Poll::Pending
        }
    }
}

impl<T> Write for EmbeddedIOAsyncAdapterUsingReady<T>
where
    T: eio::Write + eio::WriteReady + FlushCancelSafe + Unpin,
{
    fn poll_write(
        mut self: Pin<&mut Self>,
        cx: &mut Context<'_>,
        buf: &[u8],
    ) -> Poll<Result<usize, Self::Error>> {
        if self.0.write_ready()? {
            match pin!(self.0.write(buf)).poll(cx) {
                Poll::Ready(r) => Poll::Ready(r),
                Poll::Pending => unreachable!(),
            }
        } else {
            Poll::Pending
        }
    }

    fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
        pin!(self.0.flush()).poll(cx)
    }
}