Add a Source::get method for finding a single value #335

Merged
KodrAus merged 1 commit from feat/source-get into master 2019-06-30 19:12:18 -05:00
KodrAus commented 2019-06-27 22:20:35 -05:00 (Migrated from github.com)

Part of #149

Adds get to Source for fetching a single value:

fn get<'v>(&'v self, key: Key) -> Option<Value<'v>> { .. }

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.

Part of #149 Adds `get` to `Source` for fetching a single value: ```rust fn get<'v>(&'v self, key: Key) -> Option<Value<'v>> { .. } ``` 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.
Sign in to join this conversation.
No reviewers
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
gh-rust-lang/log!335
No description provided.