diff options
| -rw-r--r-- | embassy/src/io/mod.rs | 4 | ||||
| -rw-r--r-- | embassy/src/io/std.rs | 35 | ||||
| -rw-r--r-- | embassy/src/io/traits.rs | 37 |
3 files changed, 39 insertions, 37 deletions
diff --git a/embassy/src/io/mod.rs b/embassy/src/io/mod.rs index 8445f6e80..52b050971 100644 --- a/embassy/src/io/mod.rs +++ b/embassy/src/io/mod.rs | |||
| @@ -1,7 +1,11 @@ | |||
| 1 | mod error; | 1 | mod error; |
| 2 | #[cfg(feature = "std")] | ||
| 3 | mod std; | ||
| 2 | mod traits; | 4 | mod traits; |
| 3 | mod util; | 5 | mod util; |
| 4 | 6 | ||
| 5 | pub use self::error::*; | 7 | pub use self::error::*; |
| 8 | #[cfg(feature = "std")] | ||
| 9 | pub use self::std::*; | ||
| 6 | pub use self::traits::*; | 10 | pub use self::traits::*; |
| 7 | pub use self::util::*; | 11 | pub use self::util::*; |
diff --git a/embassy/src/io/std.rs b/embassy/src/io/std.rs new file mode 100644 index 000000000..ddec8d56d --- /dev/null +++ b/embassy/src/io/std.rs | |||
| @@ -0,0 +1,35 @@ | |||
| 1 | use core::pin::Pin; | ||
| 2 | use core::task::{Context, Poll}; | ||
| 3 | use futures::io as std_io; | ||
| 4 | |||
| 5 | use super::{AsyncBufRead, AsyncWrite, Result}; | ||
| 6 | |||
| 7 | pub struct FromStdIo<T>(T); | ||
| 8 | |||
| 9 | impl<T> FromStdIo<T> { | ||
| 10 | pub fn new(inner: T) -> Self { | ||
| 11 | Self(inner) | ||
| 12 | } | ||
| 13 | } | ||
| 14 | |||
| 15 | impl<T: std_io::AsyncBufRead> AsyncBufRead for FromStdIo<T> { | ||
| 16 | fn poll_fill_buf(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<&[u8]>> { | ||
| 17 | let Self(inner) = unsafe { self.get_unchecked_mut() }; | ||
| 18 | unsafe { Pin::new_unchecked(inner) } | ||
| 19 | .poll_fill_buf(cx) | ||
| 20 | .map_err(|e| e.into()) | ||
| 21 | } | ||
| 22 | fn consume(self: Pin<&mut Self>, amt: usize) { | ||
| 23 | let Self(inner) = unsafe { self.get_unchecked_mut() }; | ||
| 24 | unsafe { Pin::new_unchecked(inner) }.consume(amt) | ||
| 25 | } | ||
| 26 | } | ||
| 27 | |||
| 28 | impl<T: std_io::AsyncWrite> AsyncWrite for FromStdIo<T> { | ||
| 29 | fn poll_write(self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &[u8]) -> Poll<Result<usize>> { | ||
| 30 | let Self(inner) = unsafe { self.get_unchecked_mut() }; | ||
| 31 | unsafe { Pin::new_unchecked(inner) } | ||
| 32 | .poll_write(cx, buf) | ||
| 33 | .map_err(|e| e.into()) | ||
| 34 | } | ||
| 35 | } | ||
diff --git a/embassy/src/io/traits.rs b/embassy/src/io/traits.rs index b59cdc0d4..8e4a981da 100644 --- a/embassy/src/io/traits.rs +++ b/embassy/src/io/traits.rs | |||
| @@ -5,9 +5,6 @@ use core::task::{Context, Poll}; | |||
| 5 | #[cfg(feature = "alloc")] | 5 | #[cfg(feature = "alloc")] |
| 6 | use alloc::boxed::Box; | 6 | use alloc::boxed::Box; |
| 7 | 7 | ||
| 8 | #[cfg(feature = "std")] | ||
| 9 | use futures::io as std_io; | ||
| 10 | |||
| 11 | use super::error::Result; | 8 | use super::error::Result; |
| 12 | 9 | ||
| 13 | /// Read bytes asynchronously. | 10 | /// Read bytes asynchronously. |
| @@ -159,37 +156,3 @@ where | |||
| 159 | self.get_mut().as_mut().poll_write(cx, buf) | 156 | self.get_mut().as_mut().poll_write(cx, buf) |
| 160 | } | 157 | } |
| 161 | } | 158 | } |
| 162 | |||
| 163 | #[cfg(feature = "std")] | ||
| 164 | pub struct FromStdIo<T>(T); | ||
| 165 | |||
| 166 | #[cfg(feature = "std")] | ||
| 167 | impl<T> FromStdIo<T> { | ||
| 168 | pub fn new(inner: T) -> Self { | ||
| 169 | Self(inner) | ||
| 170 | } | ||
| 171 | } | ||
| 172 | |||
| 173 | #[cfg(feature = "std")] | ||
| 174 | impl<T: std_io::AsyncBufRead> AsyncBufRead for FromStdIo<T> { | ||
| 175 | fn poll_fill_buf(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<&[u8]>> { | ||
| 176 | let Self(inner) = unsafe { self.get_unchecked_mut() }; | ||
| 177 | unsafe { Pin::new_unchecked(inner) } | ||
| 178 | .poll_fill_buf(cx) | ||
| 179 | .map_err(|e| e.into()) | ||
| 180 | } | ||
| 181 | fn consume(self: Pin<&mut Self>, amt: usize) { | ||
| 182 | let Self(inner) = unsafe { self.get_unchecked_mut() }; | ||
| 183 | unsafe { Pin::new_unchecked(inner) }.consume(amt) | ||
| 184 | } | ||
| 185 | } | ||
| 186 | |||
| 187 | #[cfg(feature = "std")] | ||
| 188 | impl<T: std_io::AsyncWrite> AsyncWrite for FromStdIo<T> { | ||
| 189 | fn poll_write(self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &[u8]) -> Poll<Result<usize>> { | ||
| 190 | let Self(inner) = unsafe { self.get_unchecked_mut() }; | ||
| 191 | unsafe { Pin::new_unchecked(inner) } | ||
| 192 | .poll_write(cx, buf) | ||
| 193 | .map_err(|e| e.into()) | ||
| 194 | } | ||
| 195 | } | ||
