#box #traits #macro #forwarding #macro-derive

macro box-dyn

Macro to derive the implementation of Trait for Box<T: Trait>

4 releases

0.0.8 May 24, 2024
0.0.7 May 24, 2024
0.0.6 May 24, 2024
0.0.5 May 24, 2024

#847 in Rust patterns

Download history 221/week @ 2024-05-21 7/week @ 2024-05-28 2/week @ 2024-06-11

230 downloads per month

Custom license

10KB
111 lines

box-dyn

build status test status crates.io docs.rs

A simple macro, here is how to use it:

#[box_dyn::box_dyn]
trait MyTrait {
    fn method_a(&self, param: usize) -> String;
    fn method_b(&mut self);
}

This will automatically implement MyTrait for Box<T: MyTrait>:

impl<T> MyTrait for Box<T> where T: MyTrait {
    fn method_a(&self, param: usize) -> String {
        MyTrait::method_a(self.as_ref(), param)
    }
    fn method_b(&mut self) {
        MyTrait::method_b(self.as_mut())
    }
}

Dependencies

~0.5–1MB
~23K SLoC