#traits #macro #default #expand #display #collection #type

default-impl

A macro for implementing traits with their default implementations

1 unstable release

0.1.0 Jan 20, 2024

#1412 in Rust patterns


Used in 2 crates (via beetle-nonzero)

MIT license

3KB

A macro that expands to the default implementation of a trait on a collection of types.

Example

trait Show: Display + Sized {
    fn show(&self) {
        println!("{self}");
    }
}

// Uses the default implementation of Show to implement it on all the provided types.
default_impl!(Show, u8, u16, u32, String, isize);

what default_impl expands to in this case:

impl Show for u8 {}
impl Show for u16 {}
impl Show for u32 {}
impl Show for String {}
impl Show for isize {}

No runtime deps