async_hal/serial/
reader.rs

1/// Read half of a UART serial port.
2use core::{
3    marker::PhantomData,
4    pin::Pin,
5    task::{Context, Poll},
6};
7use embedded_hal::serial::Read;
8use futures::Stream;
9
10pub struct Reader<R, W> {
11    read: R,
12    _marker: PhantomData<W>,
13}
14
15impl<R, W> Reader<R, W> {
16    /// Create a new reader from an instance of [`Read`].
17    pub const fn new(read: R) -> Self {
18        Self {
19            read,
20            _marker: PhantomData,
21        }
22    }
23}
24
25impl<R, W> Unpin for Reader<R, W> {}
26
27impl<R, W> Stream for Reader<R, W>
28where
29    R: Read<W> + Unpin,
30{
31    type Item = Result<W, R::Error>;
32
33    fn poll_next(mut self: Pin<&mut Self>, _cx: &mut Context) -> Poll<Option<Self::Item>> {
34        match self.read.read() {
35            Ok(frame) => Poll::Ready(Some(Ok(frame))),
36            Err(nb::Error::WouldBlock) => Poll::Pending,
37            Err(nb::Error::Other(error)) => Poll::Ready(Some(Err(error))),
38        }
39    }
40}