io: move std stuff to own module

This commit is contained in:
Dario Nieuwenhuis 2021-08-24 23:34:43 +02:00
parent 503be49417
commit 55b2d7b524
3 changed files with 39 additions and 37 deletions

View file

@ -1,7 +1,11 @@
mod error;
#[cfg(feature = "std")]
mod std;
mod traits;
mod util;
pub use self::error::*;
#[cfg(feature = "std")]
pub use self::std::*;
pub use self::traits::*;
pub use self::util::*;

35
embassy/src/io/std.rs Normal file
View file

@ -0,0 +1,35 @@
use core::pin::Pin;
use core::task::{Context, Poll};
use futures::io as std_io;
use super::{AsyncBufRead, AsyncWrite, Result};
pub struct FromStdIo<T>(T);
impl<T> FromStdIo<T> {
pub fn new(inner: T) -> Self {
Self(inner)
}
}
impl<T: std_io::AsyncBufRead> AsyncBufRead for FromStdIo<T> {
fn poll_fill_buf(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<&[u8]>> {
let Self(inner) = unsafe { self.get_unchecked_mut() };
unsafe { Pin::new_unchecked(inner) }
.poll_fill_buf(cx)
.map_err(|e| e.into())
}
fn consume(self: Pin<&mut Self>, amt: usize) {
let Self(inner) = unsafe { self.get_unchecked_mut() };
unsafe { Pin::new_unchecked(inner) }.consume(amt)
}
}
impl<T: std_io::AsyncWrite> AsyncWrite for FromStdIo<T> {
fn poll_write(self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &[u8]) -> Poll<Result<usize>> {
let Self(inner) = unsafe { self.get_unchecked_mut() };
unsafe { Pin::new_unchecked(inner) }
.poll_write(cx, buf)
.map_err(|e| e.into())
}
}

View file

@ -5,9 +5,6 @@ use core::task::{Context, Poll};
#[cfg(feature = "alloc")]
use alloc::boxed::Box;
#[cfg(feature = "std")]
use futures::io as std_io;
use super::error::Result;
/// Read bytes asynchronously.
@ -159,37 +156,3 @@ where
self.get_mut().as_mut().poll_write(cx, buf)
}
}
#[cfg(feature = "std")]
pub struct FromStdIo<T>(T);
#[cfg(feature = "std")]
impl<T> FromStdIo<T> {
pub fn new(inner: T) -> Self {
Self(inner)
}
}
#[cfg(feature = "std")]
impl<T: std_io::AsyncBufRead> AsyncBufRead for FromStdIo<T> {
fn poll_fill_buf(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<&[u8]>> {
let Self(inner) = unsafe { self.get_unchecked_mut() };
unsafe { Pin::new_unchecked(inner) }
.poll_fill_buf(cx)
.map_err(|e| e.into())
}
fn consume(self: Pin<&mut Self>, amt: usize) {
let Self(inner) = unsafe { self.get_unchecked_mut() };
unsafe { Pin::new_unchecked(inner) }.consume(amt)
}
}
#[cfg(feature = "std")]
impl<T: std_io::AsyncWrite> AsyncWrite for FromStdIo<T> {
fn poll_write(self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &[u8]) -> Poll<Result<usize>> {
let Self(inner) = unsafe { self.get_unchecked_mut() };
unsafe { Pin::new_unchecked(inner) }
.poll_write(cx, buf)
.map_err(|e| e.into())
}
}