Crates.io | typeunion |
lib.rs | typeunion |
version | 0.1.0 |
source | src |
created_at | 2023-12-02 17:57:11.148116 |
updated_at | 2023-12-02 17:57:11.148116 |
description | Attribute macro to declare type unions in Rust |
homepage | |
repository | https://github.com/antoniusnaumann/typeunion |
max_upload_size | |
id | 1056317 |
size | 6,242 |
Instead of declaring simple type unions using an enum like this
pub enum MyTypes {
String(String),
Int(i32),
}
this macro lets you simply write this:
// Use an alias to give the enum case a custom name.
// By default, the associated type name is used.
type Int = i32;
#[type_union]
pub type MyTypes = String + Int;
It also can generate From
-implementations for subsets:
use typeunion::type_union;
#[type_union]
type SuperSet = String + TypeB + TypeC;
#[type_union(super = SuperSet)]
type SubSet = String + TypeB;
fn main() {
// `From` is automatically implemented for all types contained in SubSet
let sub_set: SubSet = "hello".to_string().into();
let super_set: SuperSet = sub.into();
}
As the name of the generated enum case is automatically derived from the given type names, only identifiers are allowed as members of a type union. To work around this, you can create a type alias:
type ArcStr = Arc<str>;
type IntVec = Vec<i64>;
#[type_union]
type Types = ArcStr + IntVec;