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

Add no_std support #244

Closed
wants to merge 3 commits into from
Closed

Add no_std support #244

wants to merge 3 commits into from

Conversation

mina86
Copy link
Contributor

@mina86 mina86 commented Aug 7, 2023

Note: When reviewing and merging the changes it’s best to look at each commit individually rather than combined changes. Each commit in the PR is self-contained with message describing what it’s doing. I can send each as separate PR if that’d be easier to handle.

This adds no_std support by introducing no_std feature. Enabling that feature requires nightly compiler thus I was also contemplating whether unstable would be a better choice.

Arguably it would be better to have a std feature instead which is enabled by default but doing that could break default-features = false builds.

Rather than having separate traits implementing as_display method,
replace DisplayAsDisplay and PathAsDisplay traits with a AsDisplay
trait.  The difference between those two traits is in the result
returned by the as_display method.  With AsDisplay trait this is
captured by an associated type.

The main motivation for the change is making it simpler to support
no_std builds in the future.  Previously, PathAsDisplay would have to
be handled specially in such builds on the side of macro expansion.
Now, thiserror-impl doesn’t need to be aware of any complications
around AsDisplay since they are all contained in thiserror crate.
Prefer core crate to std crate when expanding macros.  Like before,
the main motivation is easier support of no_std.

Two symbols needed to be handled specially.  Firstly, re-export
std::error::Error from thiserror::__private so that macro expansion
can use it without having to refer to std crate.  The idea here is
that in future commits, based on selected features, either std::error
or core::error will be used.

Secondly, leave std::backtrace::Backtrace as is since it doesn’t have
no_std equivalent.  Thankfully, the type is only used when user type
refers to it so it should not be a hindrance for no_std support.

While at it, use leading :: for core and std paths so they don’t
conflict with modules of the same name.
Introduce no_std feature which, when enabled, uses core::error::Error
trait rather than std::error::Error and thus makes it possible to use
the crate in no_std builds.

Importantly though, using core::error module requires a unstable
error_in_core feature thus enabling no_std requires building with
nightly compiler.

Furthermore, enabling `no_std` disables handling of Backtrace fields
and support for displaying paths.  Since those types aren’t available
in no_std environments this shouldn’t be an issue.
Copy link
Owner

@dtolnay dtolnay left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was rejected already in previous PRs.

@dtolnay dtolnay closed this Aug 7, 2023
@mina86
Copy link
Contributor Author

mina86 commented Aug 7, 2023

Any thoughts on getting the first two commits merged at least? They don’t change the interface of the crate but will make it easier to maintain no_std forks since the diff to upstream will be smaller.

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 this pull request may close these issues.

2 participants