Implement trivial Copy and ToOwned functions for Error<_> #1788
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.
I've been using nom in some of my projects. I've yet to dig into using
VerboseError
yet but I've been using the simpleError
quite a lot for&str
parsing. Using it insideFromStr
has been quite ergonomic.However one annoyance I discovered was when returning an error from parsing a
&str
.Doing so returns a
Result<_, Error<&str>
which is awkward to work with since now you need to add lifetime specifiers to everything if you want to return the error as is. Assuming the trait even has the flexibility to add lifetime specifiers.In my case I end up rebuilding the entire
Error
before returning it like so:I thought it would be trivial to add a more ergonomic method and instead of creating a feature request I thought I would just implement it myself. I ended up adding two methods to the
Error
struct:(I implemented them for
&mut
as well but omitted that above for brevity.)This would allow my earlier conversion to be shortened to:
The actual implementation is exceedingly simple, I have made some tests just to ensure they have the expected behaviour but there really isn't much to test.
I noticed the
#[cfg(feature = "alloc")]
attribute on the import ofToOwned
and couldn't confidently work out the reasoning behind it. Therefore I carried that attribute across to the implementations that useToOwned
. Feel free to remove that attribute if the restriction is unnecessary.