| Crates.io | type-macro-derive-tricks |
| lib.rs | type-macro-derive-tricks |
| version | 0.2.0 |
| created_at | 2025-08-09 09:37:39.199882+00 |
| updated_at | 2025-08-13 17:53:32.267892+00 |
| description | Derive macros that work with ADTs containing macros in type positions |
| homepage | |
| repository | https://github.com/yasuo-ozu/type-macro-derive-tricks |
| max_upload_size | |
| id | 1787747 |
| size | 106,024 |
This crate provides derive macros that can work with ADTs containing macros in type positions, which standard Rust derive macros cannot handle.
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]),
}
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),
}
Debug, Clone, PartialEq, etc.)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],
}
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>],
}
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]]),
}
This project is licensed under the MIT License - see the LICENSE file for details.