Crates.io | trait_deref |
lib.rs | trait_deref |
version | 0.2.2 |
created_at | 2025-01-25 16:55:39.678665+00 |
updated_at | 2025-01-28 04:33:41.519414+00 |
description | Macro that simulates inheritance in a trait. |
homepage | |
repository | https://github.com/mintlu8/trait_deref |
max_upload_size | |
id | 1530718 |
size | 20,262 |
Macro that simulates inheritance in a trait.
#[trait_deref]
trait Card {
type Data;
const IS_FIXED_COST: bool = false;
fn get_cost(&self) -> i32;
fn play(&self, data: &Self::Data);
}
struct CardCostExtension<T: Card> {
base: T,
cost: i32
}
impl_card! {
// dereferences to field self.base for missing items.
@[base: T]
impl<T: Card> Card for CardCostExtension<T> {
// overwrites some items.
fn get_cost(&self) -> i32 {
self.cost
}
const IS_FIXED_COST: bool = true;
// the rest are inherited from `base` or `T`
}
}
This crate will always be somewhat janky until macro 2.0
.
Known issues include:
We cannot magically obtain import paths of types, either use fully qualified names like
::std::sync::Arc
, use a crate::*
path, which will be transformed to $crate::*
, or
use the #[import(..)]
attribute.
crate::macro!
cannot be used (lint)Rust currently does not like this because macros can both access $crate
and put macros in $crate
,
which causes ordering issues. Either use the macro from a separate crate downstream, which sidesteps the ordering issue,
or blanket import the crate root:
use crate::*;
macro!();