Crate trait_deref

Source
Expand description

§trait_deref

Macro that simulates inheritance in a trait.

§Example

#[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`
    }
}

§Caveats

This crate will always be somewhat janky until macro 2.0. Known issues include:

  • Imports

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!();

Attribute Macros§

trait_deref
Generates a macro that fills missing trait items in an impl block by inheriting from one of its fields.