Skip to content

Commit 2c818b8

Browse files
committed
io-async: add std/alloc features, add Box/Vec impls.
1 parent a105876 commit 2c818b8

File tree

5 files changed

+73
-1
lines changed

5 files changed

+73
-1
lines changed

embedded-io-async/Cargo.toml

+8
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,13 @@ categories = [
1111
"no-std",
1212
]
1313

14+
[features]
15+
std = ["alloc", "embedded-io/std"]
16+
alloc = ["embedded-io/alloc"]
17+
1418
[dependencies]
1519
embedded-io = { version = "0.5", path = "../embedded-io" }
20+
21+
[package.metadata.docs.rs]
22+
features = ["std"]
23+
rustdoc-args = ["--cfg", "docsrs"]

embedded-io-async/src/impls/boxx.rs

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
use crate::{BufRead, Read, Seek, SeekFrom, Write};
2+
use alloc::boxed::Box;
3+
4+
#[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))]
5+
impl<T: ?Sized + Read> Read for Box<T> {
6+
#[inline]
7+
async fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> {
8+
T::read(self, buf).await
9+
}
10+
}
11+
12+
#[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))]
13+
impl<T: ?Sized + BufRead> BufRead for Box<T> {
14+
#[inline]
15+
async fn fill_buf(&mut self) -> Result<&[u8], Self::Error> {
16+
T::fill_buf(self).await
17+
}
18+
19+
#[inline]
20+
fn consume(&mut self, amt: usize) {
21+
T::consume(self, amt)
22+
}
23+
}
24+
25+
#[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))]
26+
impl<T: ?Sized + Write> Write for Box<T> {
27+
#[inline]
28+
async fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> {
29+
T::write(self, buf).await
30+
}
31+
32+
#[inline]
33+
async fn flush(&mut self) -> Result<(), Self::Error> {
34+
T::flush(self).await
35+
}
36+
}
37+
38+
#[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))]
39+
impl<T: ?Sized + Seek> Seek for Box<T> {
40+
#[inline]
41+
async fn seek(&mut self, pos: SeekFrom) -> Result<u64, Self::Error> {
42+
T::seek(self, pos).await
43+
}
44+
}

embedded-io-async/src/impls/mod.rs

+5
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,7 @@
11
mod slice_mut;
22
mod slice_ref;
3+
4+
#[cfg(feature = "alloc")]
5+
mod boxx;
6+
#[cfg(feature = "alloc")]
7+
mod vec;

embedded-io-async/src/impls/vec.rs

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
use alloc::vec::Vec;
2+
3+
use crate::Write;
4+
5+
#[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))]
6+
impl Write for Vec<u8> {
7+
#[inline]
8+
async fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> {
9+
self.extend_from_slice(buf);
10+
Ok(buf.len())
11+
}
12+
}

embedded-io-async/src/lib.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
#![no_std]
21
#![feature(async_fn_in_trait, impl_trait_projections)]
2+
#![cfg_attr(not(feature = "std"), no_std)]
33
#![cfg_attr(docsrs, feature(doc_cfg))]
44
#![warn(missing_docs)]
55
#![doc = include_str!("../README.md")]
66

7+
#[cfg(feature = "alloc")]
8+
extern crate alloc;
9+
710
mod impls;
811

912
pub use embedded_io::{Error, ErrorKind, Io, ReadExactError, SeekFrom, WriteAllError};

0 commit comments

Comments
 (0)