Crates.io | typesum |
lib.rs | typesum |
version | 0.2.0 |
source | src |
created_at | 2023-08-05 19:25:57.433707 |
updated_at | 2023-08-15 13:21:57.468145 |
description | Utilties for enums, targeted at sum types |
homepage | |
repository | https://github.com/0x00002a/typesum |
max_upload_size | |
id | 936746 |
size | 19,331 |
Utilities for working with enums, primarily aimed at sum types.
#[sumtype]
: Generate all the functions and impls you could ever need for a sum typeIf 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 enumuse typesum::kinded;
#[kinded]
enum LookAtMe {
Hello { world: String },
ImAUnit,
OrPerhapsATuple(i64, u32),
}
let look = LookAtMe::ImAUnit;
assert_eq!(look.kind(), LookAtMeKind::ImAUnit);