Skip to content

Conversation

KodrAus
Copy link
Contributor

@KodrAus KodrAus commented Jun 28, 2019

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.

@KodrAus KodrAus merged commit 569175e into rust-lang:master Jul 1, 2019
@KodrAus KodrAus deleted the feat/source-get branch July 1, 2019 00:12
EFanZh pushed a commit to EFanZh/log that referenced this pull request Jul 23, 2023
Co-authored-by: Félix Saparelli <felix@passcod.name>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant