Add a Source::get method for finding a single value #335
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.
Part of #149
Adds
get
toSource
for fetching a single value:It's useful for cases where you want to find 'well-known' key values like timestamps, errors, trace ids etc.
The method needs to be object-safe, so it takes a concrete
Key
instead of a generic type. It could take a&dyn ToKey
instead, which could have nicer ergonomics, but I opted for the simpler API to start with.The method has a default implementation that has a O(N) cost. It will always look at all key-value pairs. Implementations for types like maps or concrete structs can override this to be more efficient. For implementations that we own we could use the
Result
to early return when a match is found instead of continuing to look for a match.I've also refactored the test utilities we're using to check implementations of
Value
so they can be used in other tests too.