Skip to content

Commit 64b66b5

Browse files
authored
Merge pull request #466 from rust-embedded/io
Add embedded-io, embedded-io-async, remove serial traits.
2 parents 7e119bd + 7aaa585 commit 64b66b5

File tree

28 files changed

+1375
-134
lines changed

28 files changed

+1375
-134
lines changed

.github/workflows/clippy.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ jobs:
1515
with:
1616
# embedded-hal-async needs nightly.
1717
# Use a pinned version to avoid spontaneous breakages (new clippy lints are added often)
18-
toolchain: nightly-2022-11-22
18+
toolchain: nightly-2023-07-03
1919
components: clippy
20-
- run: cargo clippy --features=embedded-hal-bus/std -- --deny=warnings
20+
- run: cargo clippy --features=std -- --deny=warnings

.github/workflows/test.yml

+3-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ jobs:
2525
- thumbv7m-none-eabi
2626
include:
2727
- target: x86_64-unknown-linux-gnu
28-
features: embedded-hal-bus/std
28+
features: std
29+
- target: x86_64-unknown-linux-gnu
30+
features: alloc
2931

3032
steps:
3133
- uses: actions/checkout@v3

Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,6 @@ members = [
77
"embedded-hal-nb",
88
"embedded-hal-bus",
99
"embedded-can",
10+
"embedded-io",
11+
"embedded-io-async", # nightly-only
1012
]

embedded-hal-async/src/lib.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
#![doc = include_str!("../README.md")]
22
#![warn(missing_docs)]
33
#![no_std]
4-
#![allow(incomplete_features)]
54
#![feature(async_fn_in_trait, impl_trait_projections)]
65

76
pub mod delay;
87
pub mod digital;
98
pub mod i2c;
10-
pub mod serial;
119
pub mod spi;

embedded-hal-async/src/serial.rs

-27
This file was deleted.

embedded-hal-nb/src/serial.rs

+73-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,77 @@
11
//! Serial interface
22
3-
pub use embedded_hal::serial::{Error, ErrorKind, ErrorType};
3+
/// Serial error
4+
pub trait Error: core::fmt::Debug {
5+
/// Convert error to a generic serial error kind
6+
///
7+
/// By using this method, serial errors freely defined by HAL implementations
8+
/// can be converted to a set of generic serial errors upon which generic
9+
/// code can act.
10+
fn kind(&self) -> ErrorKind;
11+
}
12+
13+
impl Error for core::convert::Infallible {
14+
fn kind(&self) -> ErrorKind {
15+
match *self {}
16+
}
17+
}
18+
19+
/// Serial error kind
20+
///
21+
/// This represents a common set of serial operation errors. HAL implementations are
22+
/// free to define more specific or additional error types. However, by providing
23+
/// a mapping to these common serial errors, generic code can still react to them.
24+
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
25+
#[non_exhaustive]
26+
pub enum ErrorKind {
27+
/// The peripheral receive buffer was overrun.
28+
Overrun,
29+
/// Received data does not conform to the peripheral configuration.
30+
/// Can be caused by a misconfigured device on either end of the serial line.
31+
FrameFormat,
32+
/// Parity check failed.
33+
Parity,
34+
/// Serial line is too noisy to read valid data.
35+
Noise,
36+
/// A different error occurred. The original error may contain more information.
37+
Other,
38+
}
39+
40+
impl Error for ErrorKind {
41+
fn kind(&self) -> ErrorKind {
42+
*self
43+
}
44+
}
45+
46+
impl core::fmt::Display for ErrorKind {
47+
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
48+
match self {
49+
Self::Overrun => write!(f, "The peripheral receive buffer was overrun"),
50+
Self::Parity => write!(f, "Parity check failed"),
51+
Self::Noise => write!(f, "Serial line is too noisy to read valid data"),
52+
Self::FrameFormat => write!(
53+
f,
54+
"Received data does not conform to the peripheral configuration"
55+
),
56+
Self::Other => write!(
57+
f,
58+
"A different error occurred. The original error may contain more information"
59+
),
60+
}
61+
}
62+
}
63+
64+
/// Serial error type trait
65+
///
66+
/// This just defines the error type, to be used by the other traits.
67+
pub trait ErrorType {
68+
/// Error type
69+
type Error: Error;
70+
}
71+
72+
impl<T: ErrorType> ErrorType for &mut T {
73+
type Error = T::Error;
74+
}
475

576
/// Read half of a serial interface
677
///
@@ -40,8 +111,7 @@ impl<T: Write<Word>, Word: Copy> Write<Word> for &mut T {
40111
///
41112
/// TODO write example of usage
42113
43-
impl<Word, Error: embedded_hal::serial::Error> core::fmt::Write
44-
for dyn Write<Word, Error = Error> + '_
114+
impl<Word, Error: self::Error> core::fmt::Write for dyn Write<Word, Error = Error> + '_
45115
where
46116
Word: Copy + From<u8>,
47117
{

embedded-hal/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ pub mod delay;
8181
pub mod digital;
8282
pub mod i2c;
8383
pub mod pwm;
84-
pub mod serial;
8584
pub mod spi;
8685

8786
mod private {

embedded-hal/src/serial.rs

-98
This file was deleted.

embedded-io-async/Cargo.toml

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
[package]
2+
name = "embedded-io-async"
3+
version = "0.5.0"
4+
edition = "2021"
5+
description = "Async embedded IO traits"
6+
repository = "https://github.com/rust-embedded/embedded-hal"
7+
readme = "README.md"
8+
license = "MIT OR Apache-2.0"
9+
categories = [
10+
"embedded",
11+
"no-std",
12+
]
13+
14+
[features]
15+
std = ["alloc", "embedded-io/std"]
16+
alloc = ["embedded-io/alloc"]
17+
18+
[dependencies]
19+
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/README.md

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
[![crates.io](https://img.shields.io/crates/d/embedded-io-async.svg)](https://crates.io/crates/embedded-io-async)
2+
[![crates.io](https://img.shields.io/crates/v/embedded-io-async.svg)](https://crates.io/crates/embedded-io-async)
3+
[![Documentation](https://docs.rs/embedded-io-async/badge.svg)](https://docs.rs/embedded-io-async)
4+
5+
# `embedded-io-async`
6+
7+
Async IO traits for embedded systems.
8+
9+
This crate contains asynchronous versions of the [`embedded-io`](https://crates.io/crates/embedded-io) traits and shares its scope and design goals.
10+
11+
This project is developed and maintained by the [HAL team](https://github.com/rust-embedded/wg#the-hal-team).
12+
13+
## Minimum Supported Rust Version (MSRV)
14+
15+
This crate requires Rust nightly newer than `nightly-2022-11-22`, due to requiring support for
16+
`async fn` in traits (AFIT), which is not stable yet.
17+
18+
Keep in mind Rust nightlies can make backwards-incompatible changes to unstable features
19+
at any time.
20+
21+
## License
22+
23+
Licensed under either of
24+
25+
- Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or
26+
<http://www.apache.org/licenses/LICENSE-2.0>)
27+
- MIT license ([LICENSE-MIT](LICENSE-MIT) or <http://opensource.org/licenses/MIT>)
28+
29+
at your option.
30+
31+
### Contribution
32+
33+
Unless you explicitly state otherwise, any contribution intentionally submitted
34+
for inclusion in the work by you, as defined in the Apache-2.0 license, shall be
35+
dual licensed as above, without any additional terms or conditions.

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

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

0 commit comments

Comments
 (0)