type-sets 0.2.3

Sets implemented in the rust type-system
Documentation

Crates.io Documentation License

type-sets

Compile-time set operations on Rust tuples.

type-sets treats tuples as sets of types. You can check whether a set contains a type, compare two sets, and build new sets, all in the type system with no runtime cost. The order of the types doesn't matter, so (A, B) and (B, A) are the same set. Sets can have up to 24 members.

Operations

  • Contains<E>: the set contains the type E.
  • Subset<S>: every member of the set is also in S.
  • Superset<S>: the set contains every member of S.
  • SetEqual<R>: the two sets have the same members, in any order.
  • IsEmpty: the set has no members.
  • Insert<T, E>: the set T with E added.
  • Union<T, R>: all members of T and R.
  • Members: the members of a set as TypeIds, available at runtime.

Most of these are traits that you use as bounds:

use type_sets::Contains;

/// Only accepts messages that are in the set `S`.
fn send<S: Contains<M>, M>(message: M) {
    // ...
}

Assertions

With the assertions feature, the assert module provides functions that check set relations at compile time. They are handy in tests:

[dev-dependencies]
type-sets = { version = "0.2", features = ["assertions"] }
use type_sets::*;

assert::contains::<(i32, i16), i32>();
assert::subset::<(i32,), (i32, i64)>();
assert::superset::<(i32, i16), (i32,)>();
assert::eq::<(i32, i16), (i16, i32)>();
assert::eq::<Union<(i32,), (i16,)>, (i16, i32)>();

Read the documentation for more details.

Libraries using type-sets

  • zestors: an actor framework with Erlang/OTP-style supervision. An actor's interface is a set of message types. Sending a message checks that the interface Contains it, and addresses can be converted into dynamic addresses for any Subset of the interface.
  • axum-error-sets: typed, composable HTTP error sets for axum. Each handler declares the set of status codes it can return, and smaller sets convert into any Superset.

License

Licensed under either of MIT or Apache-2.0, at your option.