concrete-type
A Rust procedural macro library for mapping enum variants to concrete types, enabling type-level programming based on runtime enum values.
Table of Contents
Overview
concrete-type provides procedural macros that create a relationship between enum variants and specific concrete types. This enables:
- Type-level programming with enums
- Executing code with concrete type knowledge at compile time based on runtime enum values
- Generating helpful utility methods and macros for working with the concrete types
- Optionally carrying configuration data with enum variants
- Composing multiple enum types together through nested macro calls
Installation
Add this to your Cargo.toml:
[]
= "0.1.0"
Features
#[derive(Concrete)]
- Map enum variants to concrete types with
#[concrete = "path::to::Type"]attribute - Generated methods:
concrete_type_id(): Returns theTypeIdof the concrete type for a variantconcrete_type_name(): Returns the name of the concrete type as a stringwith_concrete_type(): Executes a function with knowledge of the concrete type
- Auto-generated macros for type-level dispatch using the snake_case name of the enum
#[derive(ConcreteConfig)]
- Map enum variants with configuration data to concrete types
- Each variant must have a single tuple field containing the configuration
- Generated methods:
concrete_type_id(): Returns theTypeIdof the concrete type for a variantconcrete_type_name(): Returns the name of the concrete type as a stringconfig(): Returns a reference to the configuration data
- Auto-generated macros for type-level dispatch with access to both the concrete type and config data
Examples
Basic Usage
use Concrete;
// Use the auto-generated 'exchange!' macro to work with concrete types
let exchange = Binance;
let name = exchange!;
Enums with Config Data
use ConcreteConfig;
// Define concrete types and configuration types
// Define the exchange config enum with concrete type mappings and config data
// Import the trait for access to its methods
use ExchangeApi;
// Using the auto-generated exchange_config! macro:
let config = Binance;
let name = exchange_config!;
Composing Multiple Enum Types
For more complex use cases, you can compose multiple enums together through nested macros:
use Concrete;
use PhantomData;
// Define enums that map to concrete types
// A struct with type parameters
// Implement for concrete type combinations
// Using multiple enums together with nested macros
let exchange = Binance;
let strategy = StrategyA;
let name = exchange!;
assert_eq!;
License
MIT