#bind #contextual #value #macro #deeply #threadlocals

yanked rust-context

A macro for using threadlocals to deeply bind contextual values

0.1.0 Jul 4, 2023

#11 in #contextual

MIT license

5KB
99 lines

rust-deep-bind

rust-deep-bind helps you bind a value to any function you call, without explicitly passing it through an argument.

You might use this to hold on to configuration, a request or operation ID, or anything for which you would like to use a singleton, but are concerned about all problems that come about with global state.

Example:

contextual!{
    MyCounter(MY_COUNTER): u32 = 0
}

println!("{}", MyCounter::clone()); /// 0
MyCounter::replace_within(1, || {
    println!("{}", MyCounter::clone()); /// 1
});
println!("{}", MyCounter::clone()); /// 0

No runtime deps