implicit-trait

Crates.ioimplicit-trait
lib.rsimplicit-trait
version0.3.0
sourcesrc
created_at2023-01-28 03:52:32.258143
updated_at2023-01-28 15:25:17.898955
descriptionAdd methods to foreign types with implicitly defined traits.
homepagehttps://github.com/kurtbuilds/implicit-trait
repositoryhttps://github.com/kurtbuilds/implicit-trait
max_upload_size
id770149
size5,093
Kurt Wolf (kurtbuilds)

documentation

https://docs.rs/implicit-trait

README

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");
}
Commit count: 11

cargo fmt