Skip to content

Commit eb6e078

Browse files
committed
io: add std/alloc features, add Box/Vec impls.
1 parent 6eb30c5 commit eb6e078

File tree

5 files changed

+95
-1
lines changed

5 files changed

+95
-1
lines changed

embedded-io/Cargo.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,11 @@ categories = [
1010
"embedded",
1111
"no-std",
1212
]
13+
14+
[features]
15+
std = ["alloc"]
16+
alloc = []
17+
18+
[package.metadata.docs.rs]
19+
features = ["std"]
20+
rustdoc-args = ["--cfg", "docsrs"]

embedded-io/src/impls/boxx.rs

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

embedded-io/src/impls/mod.rs

Lines changed: 5 additions & 0 deletions
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/src/impls/vec.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
use crate::{Io, Write};
2+
use alloc::vec::Vec;
3+
4+
#[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))]
5+
impl Io for Vec<u8> {
6+
type Error = core::convert::Infallible;
7+
}
8+
9+
#[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))]
10+
impl Write for Vec<u8> {
11+
#[inline]
12+
fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> {
13+
self.extend_from_slice(buf);
14+
Ok(buf.len())
15+
}
16+
17+
#[inline]
18+
fn flush(&mut self) -> Result<(), Self::Error> {
19+
Ok(())
20+
}
21+
}

embedded-io/src/lib.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1-
#![no_std]
1+
#![cfg_attr(not(feature = "std"), no_std)]
2+
#![cfg_attr(docsrs, feature(doc_cfg))]
23
#![warn(missing_docs)]
34
#![doc = include_str!("../README.md")]
45

56
use core::fmt;
67

8+
#[cfg(feature = "alloc")]
9+
extern crate alloc;
10+
711
mod impls;
812

913
/// Enumeration of possible methods to seek within an I/O object.
@@ -87,6 +91,9 @@ impl<E: fmt::Debug> fmt::Display for ReadExactError<E> {
8791
}
8892
}
8993

94+
#[cfg(feature = "std")]
95+
impl<E: fmt::Debug> std::error::Error for ReadExactError<E> {}
96+
9097
/// Error returned by [`Write::write_fmt`]
9198
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
9299
pub enum WriteFmtError<E> {
@@ -110,6 +117,9 @@ impl<E: fmt::Debug> fmt::Display for WriteFmtError<E> {
110117
}
111118
}
112119

120+
#[cfg(feature = "std")]
121+
impl<E: fmt::Debug> std::error::Error for WriteFmtError<E> {}
122+
113123
/// Error returned by [`Write::write_all`]
114124
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
115125
pub enum WriteAllError<E> {
@@ -131,6 +141,9 @@ impl<E: fmt::Debug> fmt::Display for WriteAllError<E> {
131141
}
132142
}
133143

144+
#[cfg(feature = "std")]
145+
impl<E: fmt::Debug> std::error::Error for WriteAllError<E> {}
146+
134147
/// Blocking reader.
135148
///
136149
/// Semantics are the same as [`std::io::Read`], check its documentation for details.

0 commit comments

Comments
 (0)