typesum 0.2.0

Utilties for enums, targeted at sum types
Documentation
  • Coverage
  • 100%
    8 out of 8 items documented5 out of 7 items with examples
  • Size
  • Source code size: 19.33 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 441.64 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 14s Average build duration of successful builds.
  • all releases: 14s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • 0x00002a/typesum
    3 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • 0x00002a

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

use typesum::{sumtype, TryIntoError};
#[sumtype]
#[derive(Debug, PartialEq, Eq)]
enum MyT {
    Int(i64),
    Bool(bool),
}
let mut v = MyT::Int(6);
assert!(v.is_int());
assert_eq!(v.as_int(), Some(&6));
assert_eq!(v.as_int_mut(), Some(&mut 6));
assert_eq!(v.try_as_int(), Ok(&6));
assert_eq!(v.try_as_int_mut(), Ok(&mut 6));
assert_eq!(v.try_as_bool(), Err(TryIntoError::new("MyT", "Int", "Bool")));
assert_eq!(MyT::from(false), MyT::Bool(false));
assert_eq!(MyT::from(false).try_into_int(), Err(TryIntoError::new("MyT", "Bool", "Int")));

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

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

use typesum::sumtype;
#[sumtype(from = false, impl_try_into)]
#[derive(Debug, PartialEq, Eq)]
enum Overlap {
    #[sumtype(from)]
    Int1(i64),
    Int2(i64),
    Int3(i64),
    #[sumtype(from)]
    Bool1(bool),
    Bool2(bool),
}
assert_eq!(Overlap::from(0), Overlap::Int1(0));
assert_eq!(Overlap::Int3(5).try_into(), Ok(5));
assert_eq!(Overlap::from(false), Overlap::Bool1(false));
assert_eq!(Overlap::Bool2(false).try_into(), Ok(false));

#[kinded]: Generate kind aliases for an enum

use typesum::kinded;

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