Crates.io | with-thread-local |
lib.rs | with-thread-local |
version | 1.0.0 |
source | src |
created_at | 2023-01-11 12:06:32.857897 |
updated_at | 2023-01-11 12:06:32.857897 |
description | A micro crate that simplifies a bit the use of the std macro `thread_local!` |
homepage | https://github.com/cecton/with-thread-local |
repository | https://github.com/cecton/with-thread-local |
max_upload_size | |
id | 756362 |
size | 19,700 |
A micro crate that simplifies a bit the use of the std macro thread_local!
.
extern crate regex;
use with_thread_local::with_thread_local;
use regex::Regex;
let user_input = "cat";
let (is_a_pet, needs_a_walk) = with_thread_local! {
static REGEX_PET: Regex = Regex::new(r"cat|dog").unwrap();
static REGEX_WALK: Regex = Regex::new(r"dog").unwrap();
{
let is_a_pet = REGEX_PET.is_match(user_input);
let needs_a_walk = REGEX_WALK.is_match(user_input);
(is_a_pet, needs_a_walk)
}
};
assert!(is_a_pet && !needs_a_walk);
You can also use its variant move
to move variables inside the block. Though I admit I could
not write a good example:
extern crate regex;
use with_thread_local::with_thread_local;
use regex::Regex;
let user_input = vec!["cat", "love", "dog"];
let output = with_thread_local! {
static REGEX_PET: Regex = Regex::new(r"cat|dog").unwrap();
move {
user_input
.into_iter()
.filter(|s| REGEX_PET.is_match(s))
.collect::<Vec<_>>()
}
};
assert_eq!(output, ["cat", "dog"]);