#reflection #enums

enum-values

Exposes enum values via reflection

1 unstable release

Uses new Rust 2024

0.1.0 Aug 27, 2025

#1471 in Rust patterns

Download history 3/week @ 2025-09-18 142/week @ 2025-09-25 85/week @ 2025-10-02 14/week @ 2025-10-09 8/week @ 2025-10-16 4/week @ 2025-10-23

54 downloads per month
Used in wkd-exporter

MIT/Apache

5KB

enum-values

CI Crates.io

EnumValues works with a specific subset of enums: these that have primitive discriminants. The guiding use case was automatic generation of exit codes documentation from Error enums.

use enum_values::EnumValues;

/// Mapping for relevant [`Error`] variants to an [`std::process::ExitCode`].
#[derive(EnumValues, Debug)]
#[repr(u8)]
pub enum Error {
    /// IO error.
    Io(std::io::Error) = 2,

    /// Fmt error.
    Fmt(std::fmt::Error) = 3,
}

let variants = Error::variants().collect::<Vec<_>>();
eprintln!("Variants: {variants:#?}",);
assert_eq!(variants.len(), 2);

assert_eq!("IO error.", variants[0].doc);
assert_eq!("Io", variants[0].name);
assert_eq!(2, variants[0].value);

assert_eq!("Fmt error.", variants[1].doc);
assert_eq!("Fmt", variants[1].name);
assert_eq!(3, variants[1].value);

License

This project is licensed under either of:

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~160–570KB
~13K SLoC