Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Jul 1, 2019

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