flat_enum 0.1.1

Expand nested enum into flattened enum
Documentation
  • Coverage
  • 50%
    4 out of 8 items documented1 out of 6 items with examples
  • Size
  • Source code size: 9.96 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 1.27 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 18s Average build duration of successful builds.
  • all releases: 16s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • yasuo-ozu/flat_enum
    4 1 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • yasuo-ozu

flat_enum crate Latest Version Documentation GitHub Actions

This crate expands nesting enums. See the example:

# use flat_enum::{flat, into_flat, FlatTarget};
#[derive(FlatTarget)]
pub enum Enum1<A> {
    E1(A),
    E2(),
    E3(String),
}

#[into_flat(Enum2Flat<A>)]
pub enum Enum2<A> {
    #[flatten]
    Enum1(Enum1<A>),
    E4,
}

#[flat(Enum2<A>)]
pub enum Enum2Flat<A> {}

In macro invocation, the Enum2Flat expands into something like:

pub enum Enum2Flat<A> {
    E1(A),
    E2(),
    E3(String),
    E4,
}

In this example, Enum1 and Enum2 are not required to be defined in the same crate. But Enum2 and Enum2Flat should be defined in the same context (module).

Motivation

Memory compaction

In Rust's enum representation on memory, we have std::mem::Disctiminant value in addition to the field values of each variants. If two enums are nesting, it should have two discriminants on memory. The compiler's optimization algorithm does not do such work.

This crate gives a way to generate flattened enum automatically to deal with the problem.

Syntax sugar

When using a value of nested enum types in match-like expression, the matchers are easily to become complex. The flattened enum solves that.