with-thread-local 1.0.0

A micro crate that simplifies a bit the use of the std macro `thread_local!`
Documentation
  • Coverage
  • 50%
    1 out of 2 items documented1 out of 1 items with examples
  • Size
  • Source code size: 20.55 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 1.04 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 9s Average build duration of successful builds.
  • all releases: 9s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Homepage
  • cecton/with-thread-local
    3 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • cecton

Rust Latest Version Rust 1.46+ License Docs.rs LOC Dependency Status

with-thread-local

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"]);