Skip to content

Commit b7ee113

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

File tree

1 file changed

+73
-11
lines changed

1 file changed

+73
-11
lines changed

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

Lines changed: 73 additions & 11 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

@@ -41,7 +43,7 @@ For `embedded-hal` 1.0, we decided to drop the first goal, targeting only the se
4143
- The second goal delivers much more value. Being able to use any driver together with any HAL crate, out of the box, and across the entire Rust Embedded ecosystem, is just plain awesome.
4244

4345
This refocusing on drivers is the root cause of many of the changes between `embedded-hal` 0.2 and 1.0:
44-
- [Associated type compatibiilty](#removed-traits)
46+
- [Associated type compatibility](#removed-traits)
4547
- [Trait fragmentation](#trait-organization)
4648
- [Bus/device separation](#bus-device-separation)
4749
- [Fallibility](#fallibility)
@@ -91,7 +93,7 @@ These traits have been removed in the 1.0.0 release, with no replacement for now
9193
- [`watchdog::Watchdog`][watchdog]
9294

9395
Please find a general [roadmap with further guidance here][roadmap-rm-traits] about
94-
whether and how to get these traits back in a future release
96+
whether and how to get these traits back in a future release.
9597

9698
If you are a generic driver author and need one of them, we would like to hear from you. Please add your use case to the appropriate issue for the trait affected.
9799

@@ -359,16 +361,76 @@ experiment externally, and merge when some kind of feasibility had been proven.
359361

360362
## Companion crates
361363

362-
The `embedded-hal` project now spans several crates, where some functionality has been moved out from the main `embedded-hal` crate to separate crates as detailed above. Here is the full listing of crates:
364+
The `embedded-hal` project now spans several crates, where some functionality has been moved out from the main `embedded-hal` crate to separate crates as detailed above.
365+
366+
Different crates are released independently. The main `embedded-hal-*` trait crates have reached 1.0 maturity, others will become 1.0 as time passes.
367+
368+
Here is the full listing of crates:
363369

364370
| Crate | crates.io | Docs | |
365371
|-|-|-|-|
366-
| [embedded-hal](./embedded-hal) | [![crates.io](https://img.shields.io/crates/v/embedded-hal.svg)](https://crates.io/crates/embedded-hal) | [![Documentation](https://docs.rs/embedded-hal/badge.svg)](https://docs.rs/embedded-hal) | Core traits, blocking version |
367-
| [embedded-hal-async](./embedded-hal-async) | [![crates.io](https://img.shields.io/crates/v/embedded-hal-async.svg)](https://crates.io/crates/embedded-hal-async) | [![Documentation](https://docs.rs/embedded-hal-async/badge.svg)](https://docs.rs/embedded-hal-async) | Core traits, async version |
368-
| [embedded-hal-nb](./embedded-hal-nb) | [![crates.io](https://img.shields.io/crates/v/embedded-hal-nb.svg)](https://crates.io/crates/embedded-hal-nb) | [![Documentation](https://docs.rs/embedded-hal-nb/badge.svg)](https://docs.rs/embedded-hal-nb) | Core traits, polling version using the `nb` crate |
369-
| [embedded-hal-bus](./embedded-hal-bus) | [![crates.io](https://img.shields.io/crates/v/embedded-hal-bus.svg)](https://crates.io/crates/embedded-hal-bus) | [![Documentation](https://docs.rs/embedded-hal-bus/badge.svg)](https://docs.rs/embedded-hal-bus) | Utilities for sharing SPI and I2C buses |
370-
| [embedded-can](./embedded-can) | [![crates.io](https://img.shields.io/crates/v/embedded-can.svg)](https://crates.io/crates/embedded-can) | [![Documentation](https://docs.rs/embedded-can/badge.svg)](https://docs.rs/embedded-can) | Controller Area Network (CAN) traits |
371-
| [embedded-io](./embedded-io) | [![crates.io](https://img.shields.io/crates/v/embedded-io.svg)](https://crates.io/crates/embedded-io) | [![Documentation](https://docs.rs/embedded-io/badge.svg)](https://docs.rs/embedded-io) | I/O traits (read, write, seek, etc.), blocking and nonblocking version. |
372-
| [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 |
373-
| [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`...) |
372+
| [embedded-hal](../embedded-hal) | [![crates.io](https://img.shields.io/crates/v/embedded-hal.svg)](https://crates.io/crates/embedded-hal) | [![Documentation](https://docs.rs/embedded-hal/badge.svg)](https://docs.rs/embedded-hal) | Core traits, blocking version |
373+
| [embedded-hal-async](../embedded-hal-async) | [![crates.io](https://img.shields.io/crates/v/embedded-hal-async.svg)](https://crates.io/crates/embedded-hal-async) | [![Documentation](https://docs.rs/embedded-hal-async/badge.svg)](https://docs.rs/embedded-hal-async) | Core traits, async version |
374+
| [embedded-hal-nb](../embedded-hal-nb) | [![crates.io](https://img.shields.io/crates/v/embedded-hal-nb.svg)](https://crates.io/crates/embedded-hal-nb) | [![Documentation](https://docs.rs/embedded-hal-nb/badge.svg)](https://docs.rs/embedded-hal-nb) | Core traits, polling version using the `nb` crate |
375+
| [embedded-hal-bus](../embedded-hal-bus) | [![crates.io](https://img.shields.io/crates/v/embedded-hal-bus.svg)](https://crates.io/crates/embedded-hal-bus) | [![Documentation](https://docs.rs/embedded-hal-bus/badge.svg)](https://docs.rs/embedded-hal-bus) | Utilities for sharing SPI and I2C buses |
376+
| [embedded-can](../embedded-can) | [![crates.io](https://img.shields.io/crates/v/embedded-can.svg)](https://crates.io/crates/embedded-can) | [![Documentation](https://docs.rs/embedded-can/badge.svg)](https://docs.rs/embedded-can) | Controller Area Network (CAN) traits |
377+
| [embedded-io](../embedded-io) | [![crates.io](https://img.shields.io/crates/v/embedded-io.svg)](https://crates.io/crates/embedded-io) | [![Documentation](https://docs.rs/embedded-io/badge.svg)](https://docs.rs/embedded-io) | I/O traits (read, write, seek, etc.), blocking and nonblocking version. |
378+
| [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 |
379+
| [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`...) |
380+
381+
## Supporting both 0.2 and 1.0 in the same HAL
382+
383+
It is strongly recommended that HAL implementation crates provide implementations for both the `embedded-hal` v0.2 and v1.0 traits.
384+
This allows users to use drivers using either version seamlessly.
385+
386+
The way you do it is adding a dependency on both versions in `Cargo.toml` like this:
387+
388+
```toml
389+
[dependencies]
390+
embedded-hal-02 = { package = "embedded-hal", version = "0.2.7", features = ["unproven"] }
391+
embedded-hal-1 = { package = "embedded-hal", version = "1.0" }
392+
```
393+
394+
This allows you to refer to the v0.2 traits under the `embedded_hal_02` name, and the v1.0 traits under
395+
`embedded_hal_1`. Implement both versions on the same struct. For example, for an input pin:
396+
397+
```rust
398+
/// The HAL's input pin struct
399+
struct Input {...}
400+
401+
/// Implement the v0.2 traits on the struct.
402+
impl embedded_hal_02::digital::v2::InputPin for Input {
403+
type Error = Infallible;
404+
405+
fn is_high(&self) -> Result<bool, Self::Error> {
406+
...
407+
}
408+
409+
fn is_low(&self) -> Result<bool, Self::Error> {
410+
...
411+
}
412+
}
413+
414+
/// ... and implement the v1.0 traits on the *same* struct.
415+
impl embedded_hal_1::digital::ErrorType for Input {
416+
type Error = Infallible;
417+
}
418+
419+
impl embedded_hal_1::digital::InputPin for Input {
420+
fn is_high(&mut self) -> Result<bool, Self::Error> {
421+
...
422+
}
423+
424+
fn is_low(&mut self) -> Result<bool, Self::Error> {
425+
...
426+
}
427+
}
428+
```
429+
430+
## `embedded-hal-compat`
431+
432+
For HAL implementation crates that haven't been updated yet, [embedded-hal-compat](https://github.com/ryankurte/embedded-hal-compat)
433+
provides shims to support interoperability between `embedded-hal` v0.2 and v1.0.
374434

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

0 commit comments

Comments
 (0)