Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

recommend core::error::Error for ErrorType::Error #633

Open
jordens opened this issue Sep 25, 2024 · 6 comments
Open

recommend core::error::Error for ErrorType::Error #633

jordens opened this issue Sep 25, 2024 · 6 comments
Milestone

Comments

@jordens
Copy link
Contributor

jordens commented Sep 25, 2024

As of #630 the ErrorKind structs implement Error.
As explained in #630 (comment)
its difficult to make good use of that unless the e-h traits add a core::error::Error bound to their ErrorType::Error type:
A device driver that uses a e-h trait wants to return the hal's Error from its impl of core::error::Error::source() -> Option<&dyn core::error::Error>. Returning the ErrorKind is hard/annoying/inefficient due to the need to store it. The "good" way to do that is appears to either require core::error::Error for the hal's Errors or for each device driver to implement newtype wrappers for any hal Error (which are then limited as they can only rely on Error::kind() and Debug).
Adding that bound is obviously breaking. But the burden is manageable: (1) the MSRV bump is already there (2) a trivial impl of core::error::Error will always be sufficient and in most cases even complete (3) thiserror and other crates make the job easy.

@jordens jordens changed the title require core::error::Error for Errors require core::error::Error for ErrorType::Error Sep 25, 2024
@Dirbaio Dirbaio added this to the v2.0 milestone Sep 25, 2024
@Dirbaio
Copy link
Member

Dirbaio commented Sep 25, 2024

As you mention, this is a breaking change so it'd have to be released as embedded-hal v2.0.

Even if upgrading 1.0->2.0 is easy, the real burden is the ecosystem split it causes. A HAL implementing v2.x won't work with a driver requiring v1.x. There's many lightly-maintained or unmaintained HALs/drivers, we've seen this with 0.2->1.0. Almost 1 year after the release of v1.0 there's quite a few HALs and a lot of drivers that still haven't upgraded to 1.0.

This was discussed in a WG meeting when merging #630, the conclusion was this is something we do want if we do 2.0, but it alone it's not worth the burden of doing a 2.0 release.

@jordens
Copy link
Contributor Author

jordens commented Sep 25, 2024

Maybe adding the adapters and a trait for those in a separate crate is a non-terrible work around.

@jordens
Copy link
Contributor Author

jordens commented Sep 25, 2024

https://github.com/quartiq/embedded-hal-error/blob/main/src/lib.rs

@jordens
Copy link
Contributor Author

jordens commented Sep 26, 2024

FTR and AFAICT this wasn't discussed when merging but two weeks ago.

Also note that this situation is different from the 0.2-1.0 breakage. There seem to be two other options here:

  1. Strongly recommend HALs implement core::error::Error. If/when/once most HALs do, drivers can just demand that explicitly.
  2. Introduce purely additive subtraits that demand core::error::Error and blanket impls. That way HALs don't need to implement multiple e-h traits and drivers can transition smoothly. It is a bit inflationary in terms of traits and doesn't look so nice.

(To be clear: I also think that impl core::error::Error for ErrorKind is correct and good. And I don't want another trait revolution, at least not now.)

@ryankurte
Copy link
Contributor

Strongly recommend HALs implement core::error::Error. If/when/once most HALs do, drivers can just demand that explicitly.

this seems pretty reasonable to me. no weird trait tricks, it's a nice way to push for gradual adoption, and per @Dirbaio's point the strict requirement can go in a pile for a possible (one day, maybe) 2.0.

@jordens
Copy link
Contributor Author

jordens commented Sep 27, 2024

What's the opinion on the following steps?

  1. Adding the core::error::Error recommendation to the e-h trait docstrings
  2. Moving embedded-hal-error into embedded-hal (just into the repo or even into the crate)

@jordens jordens changed the title require core::error::Error for ErrorType::Error recommend core::error::Error for ErrorType::Error Nov 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants