#variant #generate #set #enums #proc-macro #insertion #procedural

macro insert-only-set

A procedural macro to generate insert-only sets from enums

4 releases (2 breaking)

0.3.1 May 10, 2024
0.3.0 May 10, 2024
0.2.0 May 10, 2024
0.1.0 May 10, 2024

#860 in Procedural macros

MIT/Apache

8KB
105 lines

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);
    }
}

Dependencies

~1.5MB
~37K SLoC