#proc-macro #traits #dynamic-dispatch #derive

macro autotrait

Reduces boilerplate by auto-generating trait definitions from impl blocks for dynamic dispatch

15 releases

Uses new Rust 2024

0.2.1 May 8, 2025
0.2.0 May 5, 2025
0.1.12 Apr 24, 2025

#834 in Rust patterns

Download history 786/week @ 2025-04-20 223/week @ 2025-04-27 347/week @ 2025-05-04 108/week @ 2025-05-11

1,464 downloads per month

MIT/Apache

17KB
432 lines

autotrait

Sometimes you want to do dynamic dispatch. First you'd have to define a trait:

trait Trait {
    fn do_stuff(&self) -> String;
}

And then implement it on something:

struct Impl;

impl Trait for Impl {
    fn do_stuff(&self) -> String {
        // do stuff here
        todo!()
    }
}

We're repeating ourselves a bunch when doing that! What if we could just do:

struct Impl;

#[autotrait::autotrait]
impl Trait for Impl {
    fn do_stuff(&self) -> String {
        // do stuff here
        todo!()
    }
}

That way we wouldn't even have to define the trait!

Well, that's what this crates does.

Dependencies

~225KB