Crate type_sets

source ·
Expand description

Sets implemented in the rust type-system.

Crates.io Documentation

This crate allows you to create sets of types in the rust type-system. These sets can be compared using SubsetOf and SupersetOf. All traits are sealed, except for AsSet, giving guarantees that users cannot implement conflicting implementations themselves.

Sets are implemented up to 12 items.

This library was created for use in meslin, but is general enough that there could be other purposes as well.

Example

use type_sets::*;

// We can define functions, that may only be called if the parameter `T` is
// a subset or superset of another set.
fn is_subset<T: SubsetOf<Set![u32, u64]>>() {}
fn is_superset<T: SupersetOf<Set![u32, u64]>>() {}
fn contains_u64<T: Contains<u64>>() {}

// We can also use custom structs as sets
struct MySet;
impl AsSet for MySet {
    type Set = Set![u32];
}

fn main() {
    is_subset::<Set![u32, u64]>(); // compiles
    is_subset::<Set![u32]>(); // compiles
    // is_subset::<Set![u32, u64, u32]>(); // does not compile

    is_superset::<Set![u32, u64]>(); // compiles
    is_superset::<Set![u32, u64, u128]>(); // compiles
    // is_superset::<Set![u32]>(); // does not compile

    is_subset::<MySet>(); // compiles
    // is_superset::<MySet>(); // does not compile

    contains_u64::<Set![u64]>();
    // contains_u64::<Set![u32]>(); // does not compile
}

Modules

  • Sets of types from 0 to 10 elements.

Macros

  • Create a Set from a list of types.

Structs

  • Marker struct to act as a set of elements.

Traits

  • Trait that allows usage of custom types instead of [Set].
  • Indicates that a set contains element E.
  • Trait to get the members (type-ids) of a set.
  • Implemented if set is a subset of S.
  • Implemented if set is a superset of S.