#![allow(clippy::incompatible_msrv)]
use core::future::poll_fn;
use core::pin::Pin;
use futures::AsyncBufReadExt;
#[derive(Clone)]
pub struct FromFutures<T: ?Sized> {
inner: T,
}
impl<T> FromFutures<T> {
pub fn new(inner: T) -> Self {
Self { inner }
}
pub fn into_inner(self) -> T {
self.inner
}
}
impl<T: ?Sized> FromFutures<T> {
pub fn inner(&self) -> &T {
&self.inner
}
pub fn inner_mut(&mut self) -> &mut T {
&mut self.inner
}
}
impl<T: ?Sized> embedded_io::ErrorType for FromFutures<T> {
type Error = std::io::Error;
}
impl<T: futures::io::AsyncRead + Unpin + ?Sized> embedded_io_async::Read for FromFutures<T> {
async fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> {
poll_fn(|cx| Pin::new(&mut self.inner).poll_read(cx, buf)).await
}
}
impl<T: futures::io::AsyncBufRead + Unpin + ?Sized> embedded_io_async::BufRead for FromFutures<T> {
async fn fill_buf(&mut self) -> Result<&[u8], Self::Error> {
self.inner.fill_buf().await
}
fn consume(&mut self, amt: usize) {
Pin::new(&mut self.inner).consume(amt);
}
}
impl<T: futures::io::AsyncWrite + Unpin + ?Sized> embedded_io_async::Write for FromFutures<T> {
async fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> {
match poll_fn(|cx| Pin::new(&mut self.inner).poll_write(cx, buf)).await {
Ok(0) if !buf.is_empty() => Err(std::io::ErrorKind::WriteZero.into()),
Ok(n) => Ok(n),
Err(e) => Err(e),
}
}
async fn flush(&mut self) -> Result<(), Self::Error> {
poll_fn(|cx| Pin::new(&mut self.inner).poll_flush(cx)).await
}
}
impl<T: futures::io::AsyncSeek + Unpin + ?Sized> embedded_io_async::Seek for FromFutures<T> {
async fn seek(&mut self, pos: embedded_io::SeekFrom) -> Result<u64, Self::Error> {
poll_fn(move |cx| Pin::new(&mut self.inner).poll_seek(cx, pos.into())).await
}
}