insert-only-set

Crates.ioinsert-only-set
lib.rsinsert-only-set
version0.3.1
sourcesrc
created_at2024-05-10 04:52:46.948416
updated_at2024-05-10 05:59:17.658026
descriptionA procedural macro to generate insert-only sets from enums
homepagehttps://github.com/ckoopmann/insert-only-set
repositoryhttps://github.com/ckoopmann/insert-only-set
max_upload_size
id1235695
size22,197
christn (ckoopmann)

documentation

https://docs.rs/insert-only-set

README

Insert Only Set

insert_only_set is a procedural macro to generates thread-safe insert-only sets from enums in Rust. Under the hood this set is a struct with a OnceLock field for each enum variant.

Features

  • Automatically generates an insert-only set for any enum
  • Thread-safe insertions with OnceLock
  • Iterates over set variants that have been inserted

Example

use insert_only_set::InsertOnlySet;

#[derive(InsertOnlySet, Debug, PartialEq)]
pub enum Type {
    Customer,
    Employee,
}

fn main() {
    let set = Type::InsertOnlySet();

    assert!(!set.contains(Type::Customer));
    assert!(!set.contains(Type::Employee));

    assert!(set.insert(Type::Customer));
    assert!(set.contains(Type::Customer));
    assert!(!set.contains(Type::Employee));

    assert!(set.insert(Type::Employee));
    assert!(set.contains(Type::Customer));
    assert!(set.contains(Type::Employee));

    // Try to insert again, should return false
    assert!(!set.insert(Type::Customer));

    for variant in set.iter() {
        println!("{:?}", variant);
    }
}
Commit count: 10

cargo fmt