Use error types instead of String
throughout the crate
#28
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.
This PR adds error types (
enum
s with variants for different error cases that haveDebug + Display + Error
implemented) and uses them throughout the crate in place ofString
as the error type onResult
.This is a mostly mechanical change; other than fixing some typos, I used the existing error messages in the crate (i.e. the
Err(format!(...))
messages).However, though this is unlikely to affect most users (assuming the typical user of this crate is just using
.unwrap()
), it still is a breaking change.Apologies for the massive commit — I couldn't find an obvious way to split it up without introducing commits where the build fails. Hopefully reviewing the PR file by file isn't too onerous; all the changes should be local to the file they are in.
Motivation
I have a bit of a pathological use case wherein binary size is of extreme concern, to the point where the error strings in this crate (and the associated
std::fmt
machinery needed to generate them) have a noticeable impact on the final binary size1.Moving the logic that actually generates
String
s for these errors intoDisplay
impls and out of the functions in this crate's API allows the optimizer to remove these strings in cases where they are not ultimately used (i.e. where the user calls.unwrap_unchecked()
or similarly discards the errors from this crate).This change also has the benefit of removing
String
(and dependence on an allocator) from parts of this crate's API which should be helpful for any potentialno_std
support efforts.Open Questions
thiserror
as a dependency. Hopefully this is okay;thiserror
is a proc macro and does depend onsyn
but it has no runtime dependencies.#[non_exhaustive]
as a way to sidestep needing a breaking change for future additions to this crate?errors
module, I added each type to the module where it is used. I think this makes the PR easier to review (the changes to each file are self-contained) and just generally lets us avoid having to jump between files inerrors/
and elsewhere in the crate quite as much but if this is undesirable I can update this PR to have all the error types inerrors
.Footnotes
Despite using
-C panic=abort
, turning up the inlining threshold, and making liberal use ofunwrap_unchecked
, the optimizer is unable to strip these strings out. I think this is because the optimizer is unable to prove that failures in the allocations involved in formatting these error strings ultimately take the same code path as propagating the failure — i.e. the program aborts — but I am not sure; fwiw using-Z oom=panic
also does not "fix" this. ↩