Attribute Macro inherent_pub::inherent_pub

source ·
#[inherent_pub]
Expand description

Make pub methods in the annotated impl Trait for Type block into inherited methods, which can be used without having to import the trait.

mod module {
    pub trait Foo {
        fn foo(self);
        fn bar(self);
    }

    pub struct Bar;

    #[inherent_pub]
    impl Foo for Bar {
        // `foo` becomes an inherent method.
        pub fn foo(self) {}
        // `bar` is not an inherent method (not `pub`)
        fn bar(self) {}
    }
}

fn main() {
    // We didn't `use foo:Foo`, but we can still use `Bar.foo()`:
    module::Bar.foo();

    // This does not compile:
    // bar::Bar.bar();

    {
        // We need to import the trait in order to use `Bar.bar()`:
        use module::Foo;
        module::Bar.bar();
    }
}