typeunion

Crates.iotypeunion
lib.rstypeunion
version0.1.0
sourcesrc
created_at2023-12-02 17:57:11.148116
updated_at2023-12-02 17:57:11.148116
descriptionAttribute macro to declare type unions in Rust
homepage
repositoryhttps://github.com/antoniusnaumann/typeunion
max_upload_size
id1056317
size6,242
Antonius Naumann (antoniusnaumann)

documentation

README

typeunion

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;
Commit count: 6

cargo fmt