unite 0.1.2

A helper macro to compose existing types into an enum
Documentation
  • Coverage
  • 100%
    2 out of 2 items documented2 out of 2 items with examples
  • Size
  • Source code size: 12.11 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 297.86 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 4s Average build duration of successful builds.
  • all releases: 4s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Homepage
  • Zerthox/unite
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • Zerthox

Unite

A small helper macro allowing you to compose existing types into an enum.

[dependencies]
unite = "0.1"

Usage

use unite::unite;

pub struct One(bool);
pub struct Two(i32);
pub struct Three(f64);

unite! {
    // defines a new enum with a variant for each struct
    pub enum Any { One, Two, Three }
}

This expands to:

pub enum Any {
    One(One),
    Two(Two),
    Three(Three),
}

Renaming

By default the enum variants use the same name as the type, but renaming is possible.

unite! {
    enum Foo {
        SameName,
        Renamed = i32,
    }
}

Helpers

The generated enums come with helper functions to access their variants with ease. Variant names are automatically converted into snake_case for the function names.

fn foo(any: Any) {
    // checks whether the enum is a specific variant
    let is_one: bool = any.is_one();

    // attempts to cast the enum to a specific variant
    let as_two: Option<&Two> = any.as_two();
    let as_three_mut: Option<&mut Three> = any.as_three_mut();
}

The generated enums also inherently implement From<Variant>.

let any: Any = One(true).into();