Provides a single macro for generating thread-local global counters by
creating a new module with a thread-local static Cell
. Currently intended
to be used with integer types. Useful for basic ID generation.
Add the following dependency to your Cargo.toml file:
toml
[dependencies]
simple-counter = "0.1.0"
And make sure to use the #[macro_use]
annotation when importing:
```rust
extern crate simple_counter;
generate_counter!(Counter, usize);
fn main() {
// Starts at 0 by default asserteq!(Counter::next(), 0); asserteq!(Counter::next(), 1); assert_eq!(Counter::next(), 2);
// Can be set to arbitrary value Counter::set(1000); asserteq!(Counter::next(), 1000); asserteq!(Counter::next(), 1001); assert_eq!(Counter::next(), 1002);
// Or reset to 0 Counter::reset(); assert_eq!(Counter::next(), 0); } ```
Here's a simple unique temp generator for a compiler:
```rust
extern crate simple_counter;
generate_counter!(TempID, usize);
pub struct Temp { id: usize, name: String, }
impl Temp { pub fn fromstr(name: &'static str) -> Self { Temp { id: TempID::next(), name: name.tostring(), } } } ```