concrete-type-0.1.0 has been yanked.
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
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!;
Parameterized Types
For more complex use cases, you can also use multiple enums together:
use Concrete;
use PhantomData;
// Define enums that map to concrete types
// A struct with type parameters that will be resolved at runtime
// Later use multiple enum values together:
let exchange = Okx;
let strategy = StrategyB;
let name = trading_system!;
License
MIT