repr_cast
A Rust procedural macro library that enhances fieldless enums with proper conversions between enum variants and their integer representation types.
Features
- Bidirectional conversions: Convert enums to integers and integers back to enums
- Type-safe: Uses
TryFromfor fallible conversions from integers - Const-friendly: Generated methods work in const contexts
- Ergonomic: Supports both explicit and implicit discriminants
- Error handling: Provides descriptive error types for invalid conversions
- Zero overhead: All conversions are inlined and compile to efficient code
Installation
Add this to your Cargo.toml:
[]
= "0.1"
Usage
Simply add the #[repr_cast(T)] attribute to your fieldless enum, where T is the integer type you want to use:
use repr_cast;
Generated API
The macro generates the following for your enum:
#[repr(T)]- Ensures the enum has the specified memory representationFrom<Enum> for T- Converts owned enum to integerFrom<&Enum> for T- Converts enum reference to integerTryFrom<T> for Enum- Converts owned integer to enum (returnsEnumConversionErrorfor invalid values)TryFrom<&T> for Enum- Converts integer reference to enumEnum::from_repr(value: T) -> Option<Enum>- Safe conversion from integerEnum::as_repr(&self) -> T- Converts enum to integerEnumConversionError- Error type for failed conversions
Examples
Basic conversion
use repr_cast;
use TryFrom;
// Enum to integer
let status = Active;
let value: u8 = status.into; // or status.as_repr()
assert_eq!;
// Integer to enum (fallible)
let status = try_from.unwrap;
assert_eq!;
// Safe conversion
if let Some = from_repr
Signed integers
let priority = Low;
let value: i32 = priority.into;
assert_eq!;
Reference conversions
let status = Active;
// Convert owned enum (consumes the enum)
let value1: u8 = status.into;
// Convert reference (doesn't consume the enum)
let status = Active;
let value2: u8 = .into;
assert_eq!;
// Can use the enum after reference conversion
println!;
// TryFrom works with references too
let code = 1u8;
let status = try_from.unwrap;
assert_eq!;
Implicit discriminants
assert_eq!;
Const expressions
const ERROR_BASE: u16 = 400;
const SERVER_ERROR_BASE: u16 = 500;
assert_eq!;
assert_eq!;
Const contexts
const FLAG_VALUE: u8 = On.as_repr;
const MAYBE_FLAG: = from_repr;
Error handling
use TryFrom;
match try_from
Supported Integer Types
The macro works with all Rust integer types:
- Unsigned:
u8,u16,u32,u64,u128,usize - Signed:
i8,i16,i32,i64,i128,isize
Requirements
- The enum must be fieldless (all variants must be unit variants)
- The enum cannot have generics (currently)
- All discriminant values must fit in the specified integer type
The macro provides clear error messages if these requirements are not met.
Comparison with #[repr(i*)] / #[repr(u*)]
Rust's built-in #[repr(i*)] and #[repr(u*)] attributes only control memory layout. They don't provide any conversion methods. This library:
- ✅ Includes
#[repr(T)]automatically - ✅ Adds safe conversion methods
- ✅ Implements standard traits (
From,TryFrom) - ✅ Supports both owned and reference conversions
- ✅ Provides const-compatible functions
- ✅ Handles complex discriminant expressions
- ✅ Includes proper error types with helpful messages
License
This project is dual-licensed under MIT or Apache-2.0, at your option.
Contributing
Contributions are welcome! Please feel free to submit issues or pull requests.