Expand description
§type-macro-derive-tricks crate

This crate provides derive macros that can work with ADTs containing macros in type positions, which standard Rust derive macros cannot handle.
§Problem
Standard Rust derive macros fail when applied to types containing macro invocations:
ⓘ
macro_rules! typ {
($t:ty) => {($t, $t)};
}
#[derive(Clone)] // Error: derive cannot be used on items with type macros
pub enum MyEnum<S> {
Add(typ![u32]),
}
§Solution
This crate provides a procedural macro that extracts macro types, generates type aliases, and then applies standard derive macros:
use type_macro_derive_tricks::macro_derive;
macro_rules! typ {
($t:ty) => {($t, $t)};
}
#[macro_derive(Debug)]
pub enum MyEnum {
Add(typ![i32]),
Sub(typ![String]),
}
This will generate:
type __RandomName1 = (i32, i32);
type __RandomName2 = (String, String);
#[derive(Debug)]
pub enum MyEnum {
Add(__RandomName1),
Sub(__RandomName2),
}
§Features
- Works with any derive macro (
Debug
,Clone
,PartialEq
, etc.) - Supports complex generic types with lifetimes
- Handles nested macro invocations
- Generates clean, hidden type aliases
- Maintains proper generic parameter relationships
§Usage
Add this to your Cargo.toml
:
[dependencies]
type-macro-derive-tricks = "0.1.0"
Then use #[macro_derive(...)]
instead of #[derive(...)]
on types containing macro invocations in type positions:
use type_macro_derive_tricks::macro_derive;
macro_rules! MyType {
($t:ty) => { Vec<$t> };
}
#[macro_derive(Debug, Clone, PartialEq)]
pub struct MyStruct<T> {
pub field: MyType![T],
}
§Advanced Examples
§With Lifetimes
use type_macro_derive_tricks::macro_derive;
macro_rules! RefMap {
($k:ty, $v:ty) => { std::collections::HashMap<$k, $v> };
}
#[macro_derive(Debug, Clone)]
pub struct ComplexStruct<'a, T, U> {
pub data: RefMap![&'a str, Result<T, U>],
}
§Nested Macros
use type_macro_derive_tricks::macro_derive;
macro_rules! Wrapper {
($t:ty) => { Box<$t> };
}
macro_rules! Container {
($t:ty) => { Vec<$t> };
}
#[macro_derive(Debug)]
pub enum NestedEnum<T> {
Nested(Container![Wrapper![T]]),
}
§How It Works
- The macro scans the AST for macro invocations in type positions
- For each unique macro type, it generates a hidden type alias with a random name
- It replaces all macro invocations with references to these aliases
- It applies the requested derive traits to the transformed structure
- Both the type aliases and the derived implementation are output together
§Limitations
- Only works with macros that expand to valid types
- The original macro invocations are replaced in the final code
- Generated type aliases are hidden but still present in the compiled code
§License
This project is licensed under the MIT License - see the LICENSE file for details.
Attribute Macros§
- macro_
derive - Main procedural macro that handles types with macros in type positions