discrim-codegen

Crates.iodiscrim-codegen
lib.rsdiscrim-codegen
version0.1.2
sourcesrc
created_at2022-05-29 09:44:49.885044
updated_at2023-07-18 23:48:40.368999
descriptionCodegen for discrim
homepage
repositoryhttps://github.com/a-lafrance/discrim
max_upload_size
id596418
size8,693
Arthur Lafrance (a-lafrance)

documentation

README

discrim

Construct enum variants from their discriminant

NOTE: I'm no longer maintaining this due to the existence of soccer; I've pretty much absorbed what I built here into that new crate, which I'll be maintaining from now on. If this crate looks interesting to you, you should go use soccer instead (it's better, I promise).

Overview

In order to initialize certain enum values from their discriminant, you basically have to manually write a giant match block that maps from each discriminant to the corresponding variant. This is both an annoyingly manual process and a potentially unsafe one; for example, what if you forget to update a discriminant value in the match after changing it in the enum definition?

This crate addresses that by providing a way to automatically generate that initialization code. More specifically, for fieldless, non-generic enums with a #[repr(...)] specified, it provides the FromDiscriminant trait and corresponding derive macro that automatically generates the initialization code as an implementation of the trait.

This project is licensed under the MIT License.

Example

A simple example, based on the use case that inspired this crate to begin with:

use discrim::FromDiscriminant;

#[derive(Debug, FromDiscriminant)]
#[repr(u8)]
enum Opcode {
    Add, Sub, Mul, Div,
}

fn main() {
    // prints "Ok(Mul)"
    println!("{:?}", Opcode::from_discriminant(2));

    // prints "Err(5)"
    println!("{:?}", Opcode::from_discriminant(5));
}
Commit count: 17

cargo fmt