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

#[derive_more::Error] refers to std:: #261

Closed
StackOverflowExcept1on opened this issue Jun 4, 2023 · 10 comments · Fixed by #268
Closed

#[derive_more::Error] refers to std:: #261

StackOverflowExcept1on opened this issue Jun 4, 2023 · 10 comments · Fixed by #268
Assignees
Milestone

Comments

@StackOverflowExcept1on
Copy link

I did two simple libraries here: https://github.com/StackOverflowExcept1on/derive-more-issue/

Why doesn't the library provide impl core::error::Error for Error?

#![no_std]

#[derive(Debug, Eq, PartialEq, derive_more::Display, derive_more::Error)]
pub enum Error {
    #[display(fmt = "Unable to write result")]
    WriteResult,
}

pub type Result<T, E = Error> = core::result::Result<T, E>;

pub fn foo(a: i32, b: i32) -> Result<i32> {
    a.checked_add(b).ok_or_else(|| Error::WriteResult)
}
error[E0433]: failed to resolve: could not find `std` in the list of imported crates
 --> src/lib.rs:3:54
  |
3 | #[derive(Debug, Eq, PartialEq, derive_more::Display, derive_more::Error)]
  |                                                      ^^^^^^^^^^^^^^^^^^ could not find `std` in the list of imported crates
@JelteF
Copy link
Owner

JelteF commented Jun 4, 2023

The Error trait is not yet in core on stable. Currently it is only in std. So if you are compiling for a no_std environment you currently cannot use Error. It seems there's an unstable feature though to enable Error in core, so maybe we should enable that when the std crate feature is not enabled: rust-lang/rust#103765

@StackOverflowExcept1on
Copy link
Author

Could you do an auto-detect using #[cfg]?

@StackOverflowExcept1on
Copy link
Author

In the snafu crate, they did a trick with conditional compilation and snafu::Error that turned out to be compatible with std::error::Error
https://github.com/shepmaster/snafu/blob/main/src/lib.rs#L354

@tyranron tyranron added this to the 1.1.0 milestone Jun 12, 2023
@tyranron
Copy link
Collaborator

Seems reasonable to have in under feature flag.

JelteF added a commit that referenced this issue Jul 1, 2023
The `Error` derive can be made to work well for the most part in
`no_std` environments by enabling `#![feature(error_in_core)]`. This
changes the `Error` derive slightly to import `Error` and related
traits from core, when the `std` feature is disabled.

Fixes #261
JelteF added a commit that referenced this issue Jul 1, 2023
The `Error` derive can be made to work well for the most part in
`no_std` environments by enabling `#![feature(error_in_core)]`. This
changes the `Error` derive slightly to import `Error` and related
traits from core, when the `std` feature is disabled.

Fixes #261
@JelteF JelteF modified the milestones: 1.1.0, 1.0.0 Jul 1, 2023
JelteF added a commit that referenced this issue Jul 1, 2023
The `Error` derive can be made to work well for the most part in
`no_std` environments by enabling `#![feature(error_in_core)]`. This
changes the `Error` derive slightly to import `Error` and related
traits from core, when the `std` feature is disabled.

Fixes #261
@JelteF
Copy link
Owner

JelteF commented Jul 1, 2023

@StackOverflowExcept1on I created a PR that adds support for the Error derive in no_std environments in #268. It also includes some tests, but could you double check that it solves your problem?

JelteF added a commit that referenced this issue Jul 1, 2023
The `Error` derive can be made to work well for the most part in
`no_std` environments by enabling `#![feature(error_in_core)]`. This
changes the `Error` derive slightly to import `Error` and related
traits from core, when the `std` feature is disabled.

Fixes #261
@StackOverflowExcept1on
Copy link
Author

Looks good, but for example snafu has a polyfill for core::error::Error that can be used at stable rust in no_std.

@tyranron
Copy link
Collaborator

tyranron commented Jul 3, 2023

@StackOverflowExcept1on is having a polyfill critical for you? We don't want deviate much from what std/core have.

@StackOverflowExcept1on
Copy link
Author

@tyranron this is not critical, but it would be useful for stable crates

@JelteF
Copy link
Owner

JelteF commented Jul 3, 2023

I looked into using the same polyfill approach as snafu. But I don't that fits well with the ideas behind this crate, especially since the Error trait is on the path to being stabilized (although that will probably take some more time). Every Error derive crate creating its own Error polyfill is also really bad for the general ecosystem, since none of them will be able to work together. It might be better to actually have add a crate feature which uses the snafu polyfill.

JelteF added a commit that referenced this issue Jul 5, 2023
Resolves #261 

## Synopsis

The `Error` derive can be made to work well for the most part in
`no_std` environments by enabling `#![feature(error_in_core)]`. This
changes the `Error` derive slightly to import `Error` and related
traits from core, when the `std` feature is disabled.

In passing this also fixes actually running the nightly error tests.
They were not actually run anymore because there was no `build.rs` file
in the root of the repo, only in the `impl` package. So the `nightly`
config was not available in tests.

Co-authored-by: tyranron <[email protected]>
@StackOverflowExcept1on
Copy link
Author

btw rust-lang/rust#125951

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

Successfully merging a pull request may close this issue.

3 participants