dtype_variant
A Rust derive macro for creating type-safe enum variants with shared type tokens across multiple enums. This enables synchronized variant types and powerful downcasting capabilities between related enums.
Features
- 🔄 Share and synchronize variant types across multiple enums
- ✨ Type-safe downcasting of enum variants using token types
- 🔒 Compile-time validation of variant types
- 📦 Optional container type support (e.g., Vec, Box)
- 🔍 Constraint trait implementation for variant types
- 🎯 Flexible pattern matching through generated macros
- 🛠️ Convenient From implementations for variant types
For example, you can define primitive type tokens that are shared between a type enum and its corresponding data container:
This ensures that operations between related enums remain type-safe and synchronized at compile time.
Installation
Add this to your Cargo.toml:
[]
= "0.0.1"
Usage
use DType;
// First, define your token types (usually generated)
Features
Type-safe Downcasting
Access variant data with compile-time type checking:
let num = Float;
// Safe downcasting methods
let float_ref: = num.;
let float_mut: = num.;
let owned_float: = num.;
Container Types
Optionally wrap variant data in container types:
Trait Constraints
Enforce trait bounds on variant types:
Pattern Matching
Generate ergonomic pattern matching macros:
match_data!;
License
MIT
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Acknowledgements
This project was inspired by dtype_dispatch, which provides similar enum variant type dispatch functionality.