Implement TryInto for Value
#947
Open
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.
Fixes #902
This PR implements
TryIntofor convertingValueinto common types such asString,str,u64,f64,i64,(),VecandMap. I've implemented these both for ownedValueand borrowedValue.Justification and Use Case
I know there's been some skepticism from the maintainer about the need for
TryIntoimpls when one can use the full deserialization machinery to retrieve a concrete typed value from aValue, but I'd like to justify this PR by pointing to both the ergonomics of havingTryIntoand the efficiency ofTryIntoover full deserialization (especially when borrowing).For my use-case, I have a struct that delays fully deserializing all values until much later in the program.
Much deeper in the code, inputs get wrapped into a container like so:
All of this machinery provides a really nice interface to calling code, that can simply call into
Inputslike so:And importantly, because inputs can be quite large, everything is done via borrows, avoiding unnecessary cloning and allocations.
This is my specific use-case, but I'm sure there are other use-cases where having reasonable and straightforward
TryIntoimpls for Value would be ergonomic and timesaving.Thank you.