#traits #macro #declaring #time #single

macro single-trait-impl

A macro for declaring and implementing a trait at the same time

2 unstable releases

0.2.0 Jan 15, 2025
0.1.0 Mar 13, 2023

#549 in Procedural macros

Download history 8/week @ 2024-11-20 5/week @ 2024-11-27 1/week @ 2024-12-04 8/week @ 2024-12-11 4/week @ 2024-12-18 1/week @ 2025-01-01 9/week @ 2025-01-08 198/week @ 2025-01-15 63/week @ 2025-02-05

265 downloads per month

MIT license

4KB

single-trait-impl

Provides a macro for declaring and implementing a trait at the same time. This can be useful when you simply want to add functionality to an existing struct but have no intention to implement it on other structs as well.

In other words, this macro reduces the verbosity of implementing your own trait for a single struct:

#[single_trait_impl]
impl SocketAddrEx for std::net::SocketAddr {
    fn useful_function(&self) {
    }
}

This will expand to the following code:

pub trait SocketAddrEx {
    fn useful_function(&self);
}

impl SocketAddrEx for std::net::SocketAddr {
    fn useful_function(&self) {
    }
}

Dependencies

~1.5MB
~39K SLoC