#traits #implicit #methods #foreign #type #defined #implicitly

macro implicit-trait

Add methods to foreign types with implicitly defined traits

5 unstable releases

0.3.0 Jan 28, 2023
0.2.2 Jan 28, 2023
0.2.1 Jan 28, 2023
0.2.0 Jan 28, 2023
0.1.0 Jan 28, 2023

#1706 in Procedural macros

Download history 6/week @ 2024-02-26 1/week @ 2024-03-11 75/week @ 2024-04-01

76 downloads per month

MIT license

4KB

implicit-trait

Create implicit traits to add methods to existing types.

use implicit_trait::implicit_trait;

// Assume Foo is from another crate, so we can't implement methods on it.
pub struct Foo {
    pub bar: i32,
    pub baz: String,
}

// Define new methods on Foo
#[implicit_trait]
impl FooExt for Foo {
    fn bar(&self) -> i32 {
        self.bar
    }

    fn baz(&self) -> &str {
        &self.baz
    }
}

// Use the new methods
fn main() {
    let foo = Foo {
        bar: 42,
        baz: "hello".to_string(),
    };
    assert_eq!(foo.bar(), 42);
    assert_eq!(foo.baz(), "hello");
}

Dependencies

~1.5MB
~33K SLoC