Skip to content

Commit df0fd0a

Browse files
committed
docs/migration: add advice to impl both versions, add e-h-compat
1 parent cb03dda commit df0fd0a

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

docs/migrating-from-0.2-to-1.0.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
- [Removed blanket implementations](#removed-blanket-implementations)
2020
- [Cargo Features](#cargo-features)
2121
- [Companion crates](#companion-crates)
22+
- [Supporting both 0.2 and 1.0 in the same HAL](#supporting-both-02-and-10-in-the-same-hal)
23+
- [`embedded-hal-compat`](#embedded-hal-compat)
2224

2325
## Overview and reasoning
2426

@@ -372,3 +374,59 @@ The `embedded-hal` project now spans several crates, where some functionality ha
372374
| [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 |
373375
| [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`...) |
374376

377+
## Supporting both 0.2 and 1.0 in the same HAL
378+
379+
It is strongly recommended that HAL implementation crates provide implementations for both the `embedded-hal` v0.2 and v1.0 traits.
380+
This allows users to use drivers using either version seamlessly.
381+
382+
The way you do it is adding a dependency on both versions in `Cargo.toml` like this:
383+
384+
```toml
385+
[dependencies]
386+
embedded-hal-02 = { package = "embedded-hal", version = "0.2.7", features = ["unproven"] }
387+
embedded-hal-1 = { package = "embedded-hal", version = "1.0" }
388+
```
389+
390+
This allows you to refer to the v0.2 traits under the `embedded_hal_02` name, and the v1.0 traits under
391+
`embedded_hal_1`. Implement both versions on the same struct. For example, for an input pin:
392+
393+
```rust
394+
/// The HAL's input pin struct
395+
struct Input {...}
396+
397+
/// Implement the v0.2 traits on the struct.
398+
impl embedded_hal_02::digital::v2::InputPin for Input {
399+
type Error = Infallible;
400+
401+
fn is_high(&self) -> Result<bool, Self::Error> {
402+
...
403+
}
404+
405+
fn is_low(&self) -> Result<bool, Self::Error> {
406+
...
407+
}
408+
}
409+
410+
/// ... and implement the v1.0 traits on the *same* struct.
411+
impl embedded_hal_1::digital::ErrorType for Input {
412+
type Error = Infallible;
413+
}
414+
415+
impl embedded_hal_1::digital::InputPin for Input {
416+
fn is_high(&mut self) -> Result<bool, Self::Error> {
417+
...
418+
}
419+
420+
fn is_low(&mut self) -> Result<bool, Self::Error> {
421+
...
422+
}
423+
}
424+
```
425+
426+
## `embedded-hal-compat`
427+
428+
For HAL implementation crates that haven't been updated yet, [embedded-hal-compat](https://github.com/ryankurte/embedded-hal-compat)
429+
provides shims to support interoperability between `embedded-hal` v0.2 and v1.0.
430+
431+
This allows using a driver requiring v1.0 with a HAL crate implementing only v0.2 or vice-versa, (generally) without alteration.
432+
See the [docs](https://docs.rs/embedded-hal-compat/) for examples.

0 commit comments

Comments
 (0)