Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/cecton/with-thread-local
A micro crate that simplifies a bit the use of the std macro thread_local!.
https://github.com/cecton/with-thread-local
Last synced: 11 days ago
JSON representation
A micro crate that simplifies a bit the use of the std macro thread_local!.
- Host: GitHub
- URL: https://github.com/cecton/with-thread-local
- Owner: cecton
- License: mit
- Created: 2023-01-11T11:58:47.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2023-01-11T12:04:58.000Z (almost 2 years ago)
- Last Synced: 2024-03-15T00:04:27.746Z (8 months ago)
- Language: Rust
- Homepage:
- Size: 6.84 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE.Apache-2.0
Awesome Lists containing this project
README
![Rust](https://github.com/cecton/with-thread-local/actions/workflows/rust.yml/badge.svg)
[![Latest Version](https://img.shields.io/crates/v/with-thread-local.svg)](https://crates.io/crates/with-thread-local)
![Rust 1.46+](https://img.shields.io/badge/rust-1.46%2B-orange.svg)
![License](https://img.shields.io/crates/l/with-thread-local)
[![Docs.rs](https://docs.rs/with-thread-local/badge.svg)](https://docs.rs/with-thread-local)
[![LOC](https://tokei.rs/b1/github/cecton/with-thread-local)](https://github.com/cecton/with-thread-local)
[![Dependency Status](https://deps.rs/repo/github/cecton/with-thread-local/status.svg)](https://deps.rs/repo/github/cecton/with-thread-local)with-thread-local
=================A micro crate that simplifies a bit the use of the std macro `thread_local!`.
```rust
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:```rust
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::>()
}
};assert_eq!(output, ["cat", "dog"]);
```