Crates.io | deep-bind |
lib.rs | deep-bind |
version | 0.1.0 |
source | src |
created_at | 2023-07-04 10:18:13.140444 |
updated_at | 2023-07-04 10:18:13.140444 |
description | A macro for using threadlocals to deeply bind contextual values |
homepage | |
repository | |
max_upload_size | |
id | 907876 |
size | 5,357 |
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.
Create a MyCounter context, backed by a threadlocal called MY_COUNTER.
contextual!{
MyCounter(MY_COUNTER): u32 = 0
}
fn main() {
println!("{}", MyCounter::clone()); /// -> 0
MyCounter::replace_within(1, || {
println!("{}", MyCounter::clone()); /// -> 1
some_other_function(); // this function can also get `1`
});
println!("{}", MyCounter::clone()); /// -> 0
}
Internally, this crate uses thread_local!{...}
to create a threadlocal with the name in parentheses, wrapped in a RefCell
. It also creates a small utility struct with the UpperCamelCaseName you chose, to read and provide your context.