-
-
Notifications
You must be signed in to change notification settings - Fork 163
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
Support no_std #64
Support no_std #64
Conversation
Thanks for doing this, @IcyDefiance ! This will unblock some work for us in Fuchsia. |
|
||
[dev-dependencies] | ||
anyhow = "1.0" | ||
ref-cast = "1.0" | ||
rustversion = "1.0" | ||
trybuild = { version = "1.0.19", features = ["diff"] } | ||
|
||
[features] | ||
default = ["std"] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note, this is technically a breaking change if anyone was already using thiserror
with default-features = false
, as they will now have reduced functionality.
I got burned by this when I added no_std
to num
crates -- rust-num/num#297. I've seen a lot of crates brush that off though, whether deliberate or not.
Would it be possible to introduce an |
@Nemo157 an https://internals.rust-lang.org/t/can-we-move-std-error-to-core/11887/1 |
There was a PR to do that, but it was closed because it's incompatible with RFC 2504, which adds a backtrace thing. It's still possible that it'll be moved to alloc, but probably not anytime soon. Aside from that, an alloc feature may have some use because it would still leave the From traits when it's disabled, but it would be even more complicated to support and avoid breaking in the future. I don't feel like I should make that decision, but I can implement it if dtolnay is okay with it. I think I'll introduce no-std-compat tonight, though. I'll be able to eliminate the use of core that way, which will be a little easier to maintain. |
@IcyDefiance if you're talking about this thread, the concern was what to do about |
At one point it “had” support but not really. |
Consider using a shared alternate trait. See shepmaster/snafu#184 |
I wasn't even considering any eventual |
Re
I haven't found that at all the case, you need to annotate some more things The biggest thing is to just ensure you have CI covering all three variations. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR. I am concerned that this approach is brittle from a feature unification perspective. That is, if some no-std crate (particularly a library supporting std as well as no-std) has:
use thiserror::Error;
#[derive(Error, Debug)]
#[error("...")]
pub struct MyError;
impl core_error::Error for MyError {}
and anything else in the dependency graph enables thiserror/std and core-error/std then it causes breakage in the above correct code.
Same problem for:
#[cfg(feature = "std")]
impl std::error::Error for MyError {}
Thoughts? Is there a different way to do no-std support in a way that doesn't cause breakage when only part of the dependency graph has no-std in mind?
@dtolnay With respect to your first scenario, perhaps thiserror could derive With respect to your second scenario, I would think that it would be easier to have the hypothetical crate's |
Would it be workable for people if we emit an impl for "std::error::Error" unconditionally, and rely on downstream to have a "std" in scope that has whatever Error trait they want implemented? // crate root
#![no_std]
mod std {
pub mod error {
pub use core_error::Error;
// or, if you're not using sources and backtraces, maybe even:
pub trait Error {}
}
}
// module
use crate::std;
use thiserror::Error;
#[derive(Error, Debug)]
enum MyError {...} I think this cleanly eliminates all bad interactions relating to feature unification. |
@dtolnay I agree that that would probably work, although I feel like it's pretty unidiomatic. So long as we're going to ask |
Personally I really dislike "fake std" approaches (e.g. For more featured environments, I'm a big fan of building out parts of |
@tarcieri that's a surprising distinction that I haven't heard before. I would be interested to dig into why it's okay for more featured environments to piece together a custom minimal std subset, but not okay in heapless environments. Isn't using a |
This only addresses the non-problematic direction, enabling thiserror/std if downstream/std is enabled. The problems come from inability to enable downstream/std if thiserror/std is enabled. |
The distinction is more like as part of your project you intend to build out some meaningful subset of
|
If it were just about the derived |
`thiserror` doesn't work in `no_std` mode, see dtolnay/thiserror#64.
This comment has been minimized.
This comment has been minimized.
a bit off topic, but: If you'd like a crate which provides a way to derive https://github.com/yaahc/displaydoc You'll still need to add something like: #[cfg(feature = "std")]
impl std::error::Error for MyError {} ...but otherwise it will still allow you to derive |
Currently thiserror crate does not support no_std so while wait for upstream PR[1] to get merged I've used "thiserror" for std case, and for no_std thiserror is hidden under cfg_attr. [1]: dtolnay/thiserror#64
1077: NDRS-974: Get rid of failure r=mpapierski a=mpapierski Ref: https://casperlabs.atlassian.net/browse/NDRS-974 Removes failure and uses optional thiserror for std case, and for no_std (i.e. wasm32 target) is hidden behind cfg_attr. This complication happens because thiserror does not support no_std upstream but a PR is pending dtolnay/thiserror#64 Co-authored-by: Michał Papierski <[email protected]> Co-authored-by: Michał Papierski <[email protected]>
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Another alternative crate that supports managing |
This PR adds an "std" feature that is enabled by default, which allows users to disable the Error trait when needed, while keeping the Display and From traits.
This is useful for libraries that would like to support no_std while using thiserror to reduce boilerplate. For example: https://github.com/bytecodealliance/cranelift/issues/1261#issuecomment-589475011