samevariant

Crates.iosamevariant
lib.rssamevariant
version0.0.4
sourcesrc
created_at2023-01-14 13:12:32.558479
updated_at2024-07-21 19:08:13.796137
descriptionDerive enum of same-variant pairs
homepagehttps://github.com/andrewbaxter/samevariant
repositoryhttps://github.com/andrewbaxter/samevariant
max_upload_size
id758846
size11,193
Andrew Baxter (andrewbaxter)

documentation

README

Derives an enum of same-variant pairs of another enum.

Installation

cargo add samevariant

Use

Define an enum and use this derive macro:

#[samevariant(pub ABCSameVariant)]
pub enum ABC {
    A,
    B,
    C(i32),
}

It generates a new enum that looks like:

enum ABCSameVariant<'l> {
    Nonmatching(&'l ABC, &'l ABC),
    A,
    B,
    C(&'l i32, &'l i32),
}

impl ABCSameVariant {
    fn pairs<'l>(a: &'l ABC, b: &'l ABC) -> ABCSameVariant<'l> {
        ...
    }
}

If the base enum has a variant Nonmatching it will be prefixed by the original enum name (like ABCNonmatching).

Only simple and tuple variants are supported, struct-like variants will result in an error.

Commit count: 1

cargo fmt