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

macro autotrait

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

13 releases

Uses new Rust 2024

new 0.1.12 Apr 24, 2025
0.1.11 Apr 24, 2025

#769 in Rust patterns

Download history 1779/week @ 2025-04-22

1,779 downloads per month

MIT/Apache

16KB
392 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

~195KB