Skip to content

Implement with for read access to MDC#2

Open
nwtnni wants to merge 1 commit intosfackler:masterfrom
nwtnni:master
Open

Implement with for read access to MDC#2
nwtnni wants to merge 1 commit intosfackler:masterfrom
nwtnni:master

Conversation

@nwtnni
Copy link

@nwtnni nwtnni commented Feb 18, 2020

Addresses #1.

It would be fairly straightforward to implement a symmetric with_mut function as well:

pub fn with_mut<F, T>(f: F) -> T
    where F: FnOnce(&mut HashMap<String, String>) -> T
{
    MDC.with(|m| f(&mut *m.borrow_mut()))
}

I don't have an immediate use-case for this, though. I can add this in if you'd like, although it feels a little too powerful for the purposes of MDC.

Because thread-local variables aren't truly `'static`, references cannot
outlive the original `LocalKey::with` closure. This function provides
access to the entire MDC map within the scope of that closure, so we can
work with more than one key-value pair at a time without allocating.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant