typeunion 0.1.0

Attribute macro to declare type unions in Rust
Documentation
  • Coverage
  • 50%
    1 out of 2 items documented1 out of 1 items with examples
  • Size
  • Source code size: 6.85 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 284.82 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 6s Average build duration of successful builds.
  • all releases: 6s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • antoniusnaumann/typeunion
    4 1 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • antoniusnaumann

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;