3 unstable releases

Uses new Rust 2024

0.2.0 May 28, 2025
0.1.1 May 24, 2025
0.1.0 May 24, 2025

#20 in #const-context

Download history 2/week @ 2025-10-12 1/week @ 2025-10-19

105 downloads per month
Used in cenum-utils

MIT/Apache

8KB
71 lines

githubcrates-iodocs-rs

A helper crate providing derive macros for cenum_utils.

See the parent docs for more information on use.

Example

use cenum_utils_derive::ConstEnum;

#[derive(ConstEnum)]
#[repr(u8)]
enum Enum {
X,
Y,
Z
}

cenum_utils provides a minimal set of utilities for querying certain properties of enums in const contexts.


Currently, available features include:

  • Accessing the number of variants in an enum.
  • Accessing the discriminants for all of an enum's variants.
  • Accessing the names for all of an enum's variants.

Example

use cenum_utils::*;

#[derive(ConstEnum)]
#[repr(u8)]
enum Enum {
	X,
	Y,
	Z
}

fn test() {
	assert_eq!(Enum::COUNT, 3);
	assert_eq!(Enum::DISCRIMINANTS, &[0, 1, 2]);
	assert_eq!(Enum::NAMES, &["X", "Y", "Z"])
}

const fn const_test() {
	assert!(Enum::COUNT == 3);

	static NAMES: &[u8] = &[b'X', b'Y', b'Z'];

	let mut i = 0;

	while i < Enum::COUNT {
		assert!(Enum::DISCRIMINANTS[i] as usize == i);
		assert!(Enum::NAMES[i].as_bytes()[0] == NAMES[i]);
		i += 1;
	}
}

Dependencies

~180–590KB
~14K SLoC