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
use core::error::Error;
use core::pin::pin;
use core::task::Poll;
use core::{pin::Pin, task::Context};
// use std::io;
use crate::read::internals::DidRead;
use either::Either;
use futures::{AsyncRead, Future};
use pin_project::pin_project;
// use crate::MutexFuture;
#[pin_project]
pub struct SimpleAsyncReader<R: embedded_io_async::Read>
where
R: embedded_io_async::Read,
{
state: internals::State<R>,
}
impl<R: embedded_io_async::Read> SimpleAsyncReader<R> {
pub fn new(r: R) -> Self {
return Self {
state: internals::State::Idle(r, vec![]),
};
}
}
mod internals {
use super::*;
use alloc::{boxed::Box, vec::Vec};
type BoxFut<T> = Pin<Box<dyn Future<Output = T> + Send>>;
pub(crate) trait DidRead: embedded_io_async::Read + Sized {
type T: Future<Output = (Self, Vec<u8>, Result<usize, Self::Error>)>;
fn get_fut(this: Pin<&mut SimpleAsyncReader<Self>>, buf: &mut [u8]) -> Pin<Box<Self::T>>;
}
impl<R: embedded_io_async::Read> DidRead for R {
type T = impl Future<Output = (Self, Vec<u8>, Result<usize, Self::Error>)>;
fn get_fut(this: Pin<&mut SimpleAsyncReader<Self>>, buf: &mut [u8]) -> Pin<Box<Self::T>> {
let proj = this.project();
let mut state = State::Transitional;
std::mem::swap(proj.state, &mut state);
let mut fut = match state {
State::Idle(mut inner, mut internal_buf) => {
// tracing::debug!("getting new future...");
internal_buf.clear();
internal_buf.reserve(buf.len());
unsafe { internal_buf.set_len(buf.len()) }
let x = Box::pin(async move {
let res = inner.read(&mut internal_buf[..]).await;
(inner, internal_buf, res)
});
x
}
State::Pending(fut) => {
// tracing::debug!("polling existing future...");
fut
}
State::Transitional => unreachable!(),
};
return fut;
}
}
// pub type DidRead<R: embedded_io_async::Read> =
// impl Future<Output = (R, Vec<u8>, Result<usize, R::Error>)>;
pub enum State<R: embedded_io_async::Read> {
Idle(R, Vec<u8>),
Pending(Pin<Box<<R as DidRead>::T>>),
Transitional,
}
// impl<R: embedded_io_async::Read> SimpleAsyncReader<R> {
// // #[define_opaque(DidRead)]
// pub(crate) fn get_fut(self: Pin<&mut Self>, buf: &mut [u8]) -> Pin<Box<DidRead<R>>> {
// let proj = self.project();
// let mut state = State::Transitional;
// std::mem::swap(proj.state, &mut state);
// let mut fut = match state {
// State::Idle(mut inner, mut internal_buf) => {
// // tracing::debug!("getting new future...");
// internal_buf.clear();
// internal_buf.reserve(buf.len());
// unsafe { internal_buf.set_len(buf.len()) }
// let x = Box::pin(async move {
// let res = inner.read(&mut internal_buf[..]).await;
// (inner, internal_buf, res)
// });
// x
// }
// State::Pending(fut) => {
// // tracing::debug!("polling existing future...");
// fut
// }
// State::Transitional => unreachable!(),
// };
// return fut;
// }
// }
#[cfg(feature = "futures")]
impl<R> AsyncRead for SimpleAsyncReader<R>
where
// new: R must now be `'static`, since it's captured
// by the future which is, itself, `'static`.
R: embedded_io_async::Read,
R::Error: Into<std::io::Error>,
{
// #[tracing::instrument(skip(self, buf))]
fn poll_read(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut [u8],
) -> Poll<std::io::Result<usize>> {
let mut fut = DidRead::get_fut(self.as_mut(), buf);
let proj = self.project();
match fut.as_mut().poll(cx) {
Poll::Ready((inner, mut internal_buf, result)) => {
// tracing::debug!("future was ready!");
if let Ok(n) = &result {
let n = *n;
unsafe { internal_buf.set_len(n) }
let dst = &mut buf[..n];
let src = &internal_buf[..];
dst.copy_from_slice(src);
} else {
unsafe { internal_buf.set_len(0) }
}
*proj.state = State::Idle(inner, internal_buf);
Poll::Ready(result.map_err(Into::into))
}
Poll::Pending => {
// tracing::debug!("future was pending!");
*proj.state = State::Pending(fut);
Poll::Pending
}
}
}
}
}
impl<R: embedded_io_async::Read<Error = E>, E> SimpleAsyncReader<R> {
pub fn poll_read(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut [u8],
) -> Poll<Result<usize, E>> {
let mut fut = DidRead::get_fut(self.as_mut(), buf);
let proj = self.project();
match fut.as_mut().poll(cx) {
Poll::Ready((inner, mut internal_buf, result)) => {
// tracing::debug!("future was ready!");
if let Ok(n) = &result {
let n = *n;
unsafe { internal_buf.set_len(n) }
let dst = &mut buf[..n];
let src = &internal_buf[..];
dst.copy_from_slice(src);
} else {
unsafe { internal_buf.set_len(0) }
}
*proj.state = internals::State::Idle(inner, internal_buf);
Poll::Ready(result.map_err(Into::into))
}
Poll::Pending => {
// tracing::debug!("future was pending!");
*proj.state = internals::State::Pending(fut);
Poll::Pending
}
}
}
}