serial_int

Safe, easy-to-use auto-increment integers

Auto-increment integers make great unique identifers because they do not need to be large (i.e. using more memory) to prevent collisions. They are always unique until they reach their max value, mimicking the behavior of PostgreSQL's SERIAL data type.

Features

Usage

Use a generator to create unique identifiers.

```rust let mut gen = SerialGenerator::::new();

asserteq!(0, gen.generate()); asserteq!(1, gen.generate()); ```

The generator is a simple type with minimal overhead.

```rust let generatorsize = std::mem::sizeof::>();

asserteq!(1, generatorsize); ```

To support concurrency, simply use a wrapper. You can also use static ref for generators that don't have an owner.

```rust fn main() { let usersmutex = Arc::new(Mutex::new(Vec::new())); let usersclone = Arc::clone(&users_mutex);

let handle = thread::spawn(move || {
    let alice = User::new("alice@domain.xyz");
    let mary = User::new("mary@domain.xyz");
    let mut users = users_clone.lock().unwrap();

    users.push(alice);
    users.push(mary);
});

handle.join().unwrap();

let bob = User::new("bob@domain.xyz");
let fred = User::new("fred@domain.xyz");
let mut users = users_mutex.lock().unwrap();

users.push(bob);
users.push(fred);

assert_eq!(4, users.len());

}

lazystatic! { static ref userid_gen: Arc> = Arc::new(Mutex::new(SerialGenerator::new())); }

struct User { id: u32, email: String, }

impl User { pub fn new(email: &str) -> Self { User { id: useridgen.lock().unwrap().generate(), email: email.to_string(), } } } ```

Contributing

Submit a patch. If you add a new implementation of Serial, add a submodule to tests using the provided functions.