TypeSum

Utilities for working with enums, primarily aimed at sum types.

What's in it

#[sumtype]: Generate all the functions and impls you could ever need for a sum type

If you want the full list it's in the docs for the attribute

```rust use typesum::{sumtype, TryIntoError};

[sumtype]

[derive(Debug, PartialEq, Eq)]

enum MyT { Int(i64), Bool(bool), } let mut v = MyT::Int(6); assert!(v.isint()); asserteq!(v.asint(), Some(&6)); asserteq!(v.asintmut(), Some(&mut 6)); asserteq!(v.tryasint(), Ok(&6)); asserteq!(v.tryasintmut(), Ok(&mut 6)); asserteq!(v.tryasbool(), Err(TryIntoError::new("MyT", "Int", "Bool"))); asserteq!(MyT::from(false), MyT::Bool(false)); asserteq!(MyT::from(false).tryintoint(), Err(TryIntoError::new("MyT", "Bool", "Int"))); ```

Individual variants can be ignored with #[sumtype(ignore)]

```rust,compile_fail use typesum::sumtype;

[sumtype]

enum MyT { Int(i64), #[sumtype(ignore)] Empty, } let v = MyT::Empty; v.as_empty(); // doesn't exist ```

It can even work with overlapping types

```rust use typesum::sumtype;

[sumtype(from = false, impltryinto)]

[derive(Debug, PartialEq, Eq)]

enum Overlap { #[sumtype(from)] Int1(i64), Int2(i64), Int3(i64), #[sumtype(from)] Bool1(bool), Bool2(bool), } asserteq!(Overlap::from(0), Overlap::Int1(0)); asserteq!(Overlap::Int3(5).tryinto(), Ok(5)); asserteq!(Overlap::from(false), Overlap::Bool1(false)); asserteq!(Overlap::Bool2(false).tryinto(), Ok(false)); ```

#[kinded]: Generate kind aliases for an enum

```rust use typesum::kinded;

[kinded]

enum LookAtMe { Hello { world: String }, ImAUnit, OrPerhapsATuple(i64, u32), } let look = LookAtMe::ImAUnit; assert_eq!(look.kind(), LookAtMeKind::ImAUnit); ```