Module frunk::coproduct
[−]
[src]
Module that holds Coproduct data structures, traits, and implementations
Think of "Coproduct" as ad-hoc enums; allowing you to do something like this
type I32Bool = Coproduct!(i32, bool); let co1: I32Bool = into_coproduct(3); let co2: I32Bool = into_coproduct(true); // Getting stuff let get_from_1a: Option<&i32> = co1.get(); let get_from_1b: Option<&bool> = co1.get(); assert_eq!(get_from_1a, Some(&3)); assert_eq!(get_from_1b, None); let get_from_2a: Option<&i32> = co2.get(); let get_from_2b: Option<&bool> = co2.get(); assert_eq!(get_from_2a, None); assert_eq!(get_from_2b, Some(&true));Run
Or, if you want to "fold" over all possible values of a coproduct
let folder = hlist![ |&i| format!("i32 {}", i), |&b| String::from(if b { "t" } else { "f" }) ]; assert_eq!(co1.as_ref().fold(&folder), "i32 3".to_string()); assert_eq!(co2.as_ref().fold(&folder), "t".to_string()); // There is also a value consuming-variant of fold let folded = co1.fold(hlist![ |i| format!("i32 {}", i), |b| String::from(if b { "t" } else { "f" }) ]); assert_eq!(folded, "i32 3".to_string());Run
Enums
CNil |
Phantom type for signature purposes only (has no value) |
Coproduct |
Enum type representing a Coproduct. Think of this as a Result, but capable of supporting any arbitrary number of types instead of just 2. |
Traits
CoproductFoldable |
Trait for implementing "folding" a Coproduct into a value. |
CoproductSelector |
Trait for retrieving a coproduct element by type |
IntoCoproduct |
Functions
into_coproduct |
Function for returning a Coproduct from a given type |