Skip to content

Commit

Permalink
docs/migration: add advice to impl both versions, add e-h-compat
Browse files Browse the repository at this point in the history
  • Loading branch information
Dirbaio committed Dec 27, 2023
1 parent 742d6d0 commit 689c2c8
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions docs/migrating-from-0.2-to-1.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
- [Removed blanket implementations](#removed-blanket-implementations)
- [Cargo Features](#cargo-features)
- [Companion crates](#companion-crates)
- [Supporting both 0.2 and 1.0 in the same HAL](#supporting-both-02-and-10-in-the-same-hal)
- [`embedded-hal-compat`](#embedded-hal-compat)

## Overview and reasoning

Expand Down Expand Up @@ -372,3 +374,59 @@ The `embedded-hal` project now spans several crates, where some functionality ha
| [embedded-io-async](./embedded-io-async) | [![crates.io](https://img.shields.io/crates/v/embedded-io-async.svg)](https://crates.io/crates/embedded-io-async) | [![Documentation](https://docs.rs/embedded-io-async/badge.svg)](https://docs.rs/embedded-io-async) | I/O traits, async version |
| [embedded-io-adapters](./embedded-io-adapters) | [![crates.io](https://img.shields.io/crates/v/embedded-io-adapters.svg)](https://crates.io/crates/embedded-io-adapters) | [![Documentation](https://docs.rs/embedded-io-adapters/badge.svg)](https://docs.rs/embedded-io-adapters) | Adapters between the [`embedded-io`](https://crates.io/crates/embedded-io) and [`embedded-io-async`](https://crates.io/crates/embedded-io-async) traits and other IO traits (`std`, `tokio`, `futures`...) |

## Supporting both 0.2 and 1.0 in the same HAL

It is strongly recommended that HAL implementation crates provide implementations for both the `embedded-hal` v0.2 and v1.0 traits.
This allows users to use drivers using either version seamlessly.

The way you do it is adding a dependency on both versions in `Cargo.toml` like this:

```toml
[dependencies]
embedded-hal-02 = { package = "embedded-hal", version = "0.2.7", features = ["unproven"] }
embedded-hal-1 = { package = "embedded-hal", version = "1.0" }
```

This allows you to refer to the v0.2 traits under the `embedded_hal_02` name, and the v1.0 traits under
`embedded_hal_1`. Implement both versions on the same struct. For example, for an input pin:

```rust
/// The HAL's input pin struct
struct Input {...}

/// Implement the v0.2 traits on the struct.
impl embedded_hal_02::digital::v2::InputPin for Input {
type Error = Infallible;

fn is_high(&self) -> Result<bool, Self::Error> {
...
}

fn is_low(&self) -> Result<bool, Self::Error> {
...
}
}

/// ... and implement the v1.0 traits on the *same* struct.
impl embedded_hal_1::digital::ErrorType for Input {
type Error = Infallible;
}

impl embedded_hal_1::digital::InputPin for Input {
fn is_high(&mut self) -> Result<bool, Self::Error> {
...
}

fn is_low(&mut self) -> Result<bool, Self::Error> {
...
}
}
```

## `embedded-hal-compat`

For HAL implementation crates that haven't been updated yet, [embedded-hal-compat](https://github.com/ryankurte/embedded-hal-compat)
provides shims to support interoperability between `embedded-hal` v0.2 and v1.0.

This allows using a driver requiring v1.0 with a HAL crate implementing only v0.2 or vice-versa, (generally) without alteration.
See the [docs](https://docs.rs/embedded-hal-compat/) for examples.

0 comments on commit 689c2c8

Please sign in to comment.