#variant #enums #data #value #store #structure #set

variant-set

A set-like data structure for enum variants, allowing you to store at most one value for each variant of an enum

1 unstable release

0.1.0 Oct 14, 2024

#768 in Data structures

Download history 154/week @ 2024-10-11 19/week @ 2024-10-18

173 downloads per month

CC0 license

29KB
185 lines

variant-set

variant-set provides a set-like data structure for enum variants. It allows you to store at most one value for each variant of an enum, providing efficient storage and retrieval based on enum variants.

Use-case

Suppose you have a large, complex enum such as the following:

enum MyEnum {
    Variant1(u32),
    Variant2(String),
    Variant3(f64),
    Variant4(bool),
    Variant5(Vec<u8>),
    Variant6 {
        field1: u32,
        field2: String,
    },
    Variant7(SomeLargeStruct),
}

You could store the data of this enum like so:

struct MyStruct {
    variant1: Option<u32>,
    variant2: Option<String>,
    variant3: Option<f64>,
    variant4: Option<bool>,
    variant5: Option<Vec<u8>>,
    variant6: Option<(u32, String)>,
    variant7: Option<SomeLargeStruct>,
}

However, the size of this struct is the sum of the sizes of all the fields, even if only one field is actually used. For large enough structs, this can be inefficient.

You effectively want a HashSet<MyEnum> where you can store at most one value for each variant. However, you do not care about the specific value stored for each variant, only that you can store and retrieve it efficiently based on the variant itself. This is what variant-set provides. Just derive the VariantEnum trait for your enum, and you can use the VariantSet data structure:

use variant_set::{VariantSet, VariantEnum};

#[derive(VariantEnum)]
enum MyEnum {
    Variant1(u32),
    Variant2(String),
    Variant3(f64),
    Variant4(bool),
    Variant5(Vec<u8>),
    Variant6 {
        field1: u32,
        field2: String,
    },
    Variant7(SomeLargeStruct),
}

fn main() {
    let mut set = VariantSet::new();
    set.set(MyEnum::Variant1(42));
    set.set(MyEnum::Variant2("hello".to_string()));

    assert!(set.contains(MyEnumVariant::Variant1));
    assert!(set.contains(MyEnumVariant::Variant2));
    assert!(!set.contains(MyEnumVariant::Variant3));

    let value = set.get(MyEnumVariant::Variant1);
    assert_eq!(value, Some(&MyEnum::Variant1(42)));

    let value = set.get(MyEnumVariant::Variant2);
    assert_eq!(value, Some(&MyEnum::Variant2("hello".to_string())));
}

Installation

Add to your Cargo.toml:

[dependencies]
variant-set = "0.1.0"

API Documentation

See the docs.rs documentation here.

Contributing

Contributions are welcome! Please feel free to open an issue or a pull request on GitHub.

License

This project is licensed under the CC0 License - see the LICENSE file for details.

Dependencies

~255–700KB
~17K SLoC