Skip to content

Commit

Permalink
implement core::error::Error
Browse files Browse the repository at this point in the history
this trait has been stabilised in Rust 1.81.0.

the existing custom `Error` types cannot be removed / replaced as that'd
be a breaking change. for the same reason it's not possible for them to
require `core::error::Error` being implemented.

however, we can add an `impl core::error::Error for dyn Error` for every
custom error type to provide an automatic coverage. HALs can still add
an implementation for their specific error type if they wish to add more
details (e.g. implement `source`).
as `core::error::Error` requires `core::fmt::Display` to be implemented
a simple blanket implementation has been added which prints the `Debug`
output.

existing `std` feature-gated implementations of `std::error::Error` have
also been moved to `core::error::Error` and the feature gate removed.

this raises the MSRV to 1.81.0 for most crates, but based on the MSRV
policy this should not be an issue.
  • Loading branch information
rursprung committed Sep 9, 2024
1 parent 0c31d81 commit 7bb7ca3
Show file tree
Hide file tree
Showing 26 changed files with 89 additions and 45 deletions.
18 changes: 2 additions & 16 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,23 +38,9 @@ jobs:
--target thumbv7m-none-eabi
--features async,defmt-03
msrv-1-60:
msrv-1-81:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: dtolnay/[email protected]
- run: >
cargo test
-p embedded-hal:1.0.0
-p embedded-hal-bus
-p embedded-hal-nb
-p embedded-io
-p embedded-io-adapters
-p embedded-can
msrv-1-75:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: dtolnay/[email protected]
- uses: dtolnay/[email protected]
- run: cargo test --workspace --all-features
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ on crates.io.

## Minimum Supported Rust Version (MSRV)

This crate is guaranteed to compile on stable Rust 1.60 and up. It *might*
This crate is guaranteed to compile on stable Rust 1.81 and up. It *might*
compile with older versions but that may change in any new patch release.

See [here](docs/msrv.md) for details on how the MSRV may be upgraded.
Expand Down
3 changes: 2 additions & 1 deletion embedded-can/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

...
- Added blanket `core::error::Error` and `core::fmt::Display` implementations for the custom `Error` traits
- Increased MSRV to 1.81 due to `core::error::Error`

## [v0.4.1] - 2022-09-28

Expand Down
2 changes: 1 addition & 1 deletion embedded-can/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "embedded-can"
version = "0.4.1"
edition = "2021"
rust-version = "1.56"
rust-version = "1.81"

description = "HAL traits for Controller Area Network (CAN) devices."
categories = ["embedded", "hardware-support", "no-std"]
Expand Down
2 changes: 1 addition & 1 deletion embedded-can/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ This project is developed and maintained by the [HAL team](https://github.com/ru

## Minimum Supported Rust Version (MSRV)

This crate is guaranteed to compile on stable Rust 1.60 and up. It *might*
This crate is guaranteed to compile on stable Rust 1.81 and up. It *might*
compile with older versions but that may change in any new patch release.

See [here](../docs/msrv.md) for details on how the MSRV may be upgraded.
Expand Down
8 changes: 8 additions & 0 deletions embedded-can/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ pub trait Error: core::fmt::Debug {
fn kind(&self) -> ErrorKind;
}

impl core::fmt::Display for dyn Error {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "{:?}", self)
}
}

impl core::error::Error for dyn Error {}

impl Error for core::convert::Infallible {
fn kind(&self) -> ErrorKind {
match *self {}
Expand Down
2 changes: 2 additions & 0 deletions embedded-hal-bus/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

- Added the `alloc` feature.
- Added a new `RcDevice` for I2C and SPI, a reference-counting equivalent to `RefCellDevice`.
- Migrated `std` feature-gated `std::error::Error` implementations to `core::error::Error`
- Increased MSRV to 1.81 due to `core::error::Error`

## [v0.2.0] - 2024-04-23

Expand Down
2 changes: 1 addition & 1 deletion embedded-hal-bus/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ repository = "https://github.com/rust-embedded/embedded-hal"
version = "0.2.0"

[features]
# Enable shared bus implementations using `std::sync::Mutex`, and implement `std::error::Error` for `DeviceError`
# Enable shared bus implementations using `std::sync::Mutex`
std = ["alloc"]
# Use `portable-atomic` to enable `atomic-device` on devices without native atomic CAS
#
Expand Down
5 changes: 2 additions & 3 deletions embedded-hal-bus/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,11 @@ provides mechanisms to obtain multiple `I2c` instances out of a single `I2c` ins
that does not natively support atomic CAS. If you enable this, you must also add `portable-atomic` to your crate with
a feature flag such as `unsafe-assume-single-core` or `critical-section` to choose how atomic CAS is implemented.
See <https://docs.rs/portable-atomic/1.7.0/portable_atomic/#optional-features> for more info.
- **`std`**: enable shared bus implementations using `std::sync::Mutex`, and implement
`std::error::Error` for `DeviceError`.
- **`std`**: enable shared bus implementations using `std::sync::Mutex`.

## Minimum Supported Rust Version (MSRV)

This crate is guaranteed to compile on stable Rust 1.60 and up. It *might*
This crate is guaranteed to compile on stable Rust 1.81 and up. It *might*
compile with older versions but that may change in any new patch release.

See [here](../docs/msrv.md) for details on how the MSRV may be upgraded.
Expand Down
3 changes: 1 addition & 2 deletions embedded-hal-bus/src/spi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ impl<BUS: Display, CS: Display> Display for DeviceError<BUS, CS> {
}
}

#[cfg(feature = "std")]
impl<BUS: Debug + Display, CS: Debug + Display> std::error::Error for DeviceError<BUS, CS> {}
impl<BUS: Debug + Display, CS: Debug + Display> core::error::Error for DeviceError<BUS, CS> {}

impl<BUS, CS> Error for DeviceError<BUS, CS>
where
Expand Down
3 changes: 2 additions & 1 deletion embedded-hal-nb/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

No unreleased changes
- Added blanket `core::error::Error` and `core::fmt::Display` implementations for the custom `Error` traits
- Increased MSRV to 1.81 due to `core::error::Error`

## [v1.0.0] - 2023-12-28

Expand Down
2 changes: 1 addition & 1 deletion embedded-hal-nb/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "embedded-hal-nb"
version = "1.0.0"
edition = "2021"
rust-version = "1.56"
rust-version = "1.81"

categories = ["embedded", "hardware-support", "no-std"]
description = "Non-blocking Hardware Abstraction Layer (HAL) for embedded systems using the `nb` crate."
Expand Down
2 changes: 1 addition & 1 deletion embedded-hal-nb/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ This project is developed and maintained by the [HAL team](https://github.com/ru

## Minimum Supported Rust Version (MSRV)

This crate is guaranteed to compile on stable Rust 1.60 and up. It *might*
This crate is guaranteed to compile on stable Rust 1.81 and up. It *might*
compile with older versions but that may change in any new patch release.

See [here](../docs/msrv.md) for details on how the MSRV may be upgraded.
Expand Down
8 changes: 8 additions & 0 deletions embedded-hal-nb/src/serial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ pub trait Error: core::fmt::Debug {
fn kind(&self) -> ErrorKind;
}

impl core::fmt::Display for dyn Error {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "{:?}", self)
}
}

impl core::error::Error for dyn Error {}

impl Error for core::convert::Infallible {
#[inline]
fn kind(&self) -> ErrorKind {
Expand Down
3 changes: 2 additions & 1 deletion embedded-hal/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

No unreleased changes yet.
- Added blanket `core::error::Error` and `core::fmt::Display` implementations for the custom `Error` traits
- Increased MSRV to 1.81 due to `core::error::Error`

## [v1.0.0] - 2023-12-28

Expand Down
2 changes: 1 addition & 1 deletion embedded-hal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ categories = ["asynchronous", "embedded", "hardware-support", "no-std"]
description = " A Hardware Abstraction Layer (HAL) for embedded systems "
documentation = "https://docs.rs/embedded-hal"
edition = "2021"
rust-version = "1.60"
rust-version = "1.81"
keywords = ["hal", "IO"]
license = "MIT OR Apache-2.0"
name = "embedded-hal"
Expand Down
2 changes: 1 addition & 1 deletion embedded-hal/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ to your crate before publishing it!

## Minimum Supported Rust Version (MSRV)

This crate is guaranteed to compile on stable Rust 1.60 and up. It *might*
This crate is guaranteed to compile on stable Rust 1.81 and up. It *might*
compile with older versions but that may change in any new patch release.

See [here](../docs/msrv.md) for details on how the MSRV may be upgraded.
Expand Down
8 changes: 8 additions & 0 deletions embedded-hal/src/digital.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ pub trait Error: core::fmt::Debug {
fn kind(&self) -> ErrorKind;
}

impl core::fmt::Display for dyn Error {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "{:?}", self)
}
}

impl core::error::Error for dyn Error {}

impl Error for core::convert::Infallible {
fn kind(&self) -> ErrorKind {
match *self {}
Expand Down
8 changes: 8 additions & 0 deletions embedded-hal/src/i2c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,14 @@ pub trait Error: core::fmt::Debug {
fn kind(&self) -> ErrorKind;
}

impl core::fmt::Display for dyn Error {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "{:?}", self)
}
}

impl core::error::Error for dyn Error {}

impl Error for core::convert::Infallible {
#[inline]
fn kind(&self) -> ErrorKind {
Expand Down
8 changes: 8 additions & 0 deletions embedded-hal/src/pwm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ pub trait Error: core::fmt::Debug {
fn kind(&self) -> ErrorKind;
}

impl core::fmt::Display for dyn Error {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "{:?}", self)
}
}

impl core::error::Error for dyn Error {}

impl Error for core::convert::Infallible {
#[inline]
fn kind(&self) -> ErrorKind {
Expand Down
8 changes: 8 additions & 0 deletions embedded-hal/src/spi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,14 @@ pub trait Error: Debug {
fn kind(&self) -> ErrorKind;
}

impl core::fmt::Display for dyn Error {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "{:?}", self)
}
}

impl core::error::Error for dyn Error {}

impl Error for core::convert::Infallible {
#[inline]
fn kind(&self) -> ErrorKind {
Expand Down
7 changes: 7 additions & 0 deletions embedded-io/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased

- Added blanket `core::error::Error` and `core::fmt::Display` implementations for the custom `Error` traits
- Migrated `std` feature-gated `std::error::Error` implementations to `core::error::Error`
- Increased MSRV to 1.81 due to `core::error::Error`


## 0.6.1 - 2023-10-22

- Make `SliceWriteError` publicly available.
Expand Down
2 changes: 1 addition & 1 deletion embedded-io/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "embedded-io"
version = "0.6.1"
edition = "2021"
rust-version = "1.60"
rust-version = "1.81"
description = "Embedded IO traits"
repository = "https://github.com/rust-embedded/embedded-hal"
readme = "README.md"
Expand Down
4 changes: 2 additions & 2 deletions embedded-io/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ targets.

## Optional Cargo features

- **`std`**: Adds `From` impls to convert to/from `std::io` structs, adds `std::error::Error` impls.
- **`std`**: Adds `From` impls to convert to/from `std::io` structs.
- **`alloc`**: Adds blanket impls for `Box`, adds `Write` impl to `Vec`.
- **`defmt-03`**: Derive `defmt::Format` from `defmt` 0.3 for enums and structs.

## Minimum Supported Rust Version (MSRV)

This crate is guaranteed to compile on stable Rust 1.60 and up. It *might*
This crate is guaranteed to compile on stable Rust 1.81 and up. It *might*
compile with older versions but that may change in any new patch release.

See [here](../docs/msrv.md) for details on how the MSRV may be upgraded.
Expand Down
4 changes: 0 additions & 4 deletions embedded-io/src/impls/slice_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@ impl core::fmt::Display for SliceWriteError {
}
}

#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
impl std::error::Error for SliceWriteError {}

/// Write is implemented for `&mut [u8]` by copying into the slice, overwriting
/// its data.
///
Expand Down
16 changes: 10 additions & 6 deletions embedded-io/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,14 @@ pub trait Error: fmt::Debug {
fn kind(&self) -> ErrorKind;
}

impl fmt::Display for dyn Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:?}", self)
}
}

impl core::error::Error for dyn Error {}

impl Error for core::convert::Infallible {
fn kind(&self) -> ErrorKind {
match *self {}
Expand Down Expand Up @@ -255,9 +263,7 @@ impl<E: fmt::Debug> fmt::Display for ReadExactError<E> {
}
}

#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
impl<E: fmt::Debug> std::error::Error for ReadExactError<E> {}
impl<E: fmt::Debug> core::error::Error for ReadExactError<E> {}

/// Errors that could be returned by `Write` on `&mut [u8]`.
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
Expand Down Expand Up @@ -290,9 +296,7 @@ impl<E: fmt::Debug> fmt::Display for WriteFmtError<E> {
}
}

#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
impl<E: fmt::Debug> std::error::Error for WriteFmtError<E> {}
impl<E: fmt::Debug> core::error::Error for WriteFmtError<E> {}

/// Blocking reader.
///
Expand Down

0 comments on commit 7bb7ca3

Please sign in to comment.