Closed
Conversation
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.
dtolnay
reviewed
Aug 7, 2023
Owner
dtolnay
left a comment
There was a problem hiding this comment.
This was rejected already in previous PRs.
Contributor
Author
|
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. |
Closed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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_stdfeature. Enabling that feature requires nightly compiler thus I was also contemplating whetherunstablewould be a better choice.Arguably it would be better to have a
stdfeature instead which is enabled by default but doing that could breakdefault-features = falsebuilds.