Skip to main content

impl_interface

Attribute Macro impl_interface 

#[impl_interface]
Expand description

Implement the crate interface for a struct.

This attribute should be added above the implementation of a trait for a struct, and the trait must be defined with #[def_interface].

It is not necessary to implement it in the same crate as the definition, but it is required that these crates are linked together.

See the crate-level documentation for more details.

§Restrictions

§No Alias

The specified trait name must not be an alias to the originally defined name; otherwise, it will result in a compile error.

#[def_interface]
trait MyIf {
    fn foo();
}

use MyIf as MyIf2;
struct MyImpl;
#[impl_interface]
impl MyIf2 for MyImpl {
    fn foo() {}
}

§No Namespace Mismatch

It’s also mandatory to match the namespace if one is specified when defining the interface. For example, the following will result in a compile error:

#[def_interface(namespace = MyNs)]
trait MyIf {
    fn foo();
}

struct MyImpl;

#[impl_interface(namespace = OtherNs)] // error: namespace does not match
impl MyIf for MyImpl {
    fn foo() {}
}

§No Receivers

Methods with receivers (self, &self, &mut self) are not allowed in the implementation either:

trait MyIf {
    fn foo(&self);
}

struct MyImpl;

#[impl_interface]
impl MyIf for MyImpl {
    fn foo(&self) {} // error: methods with receiver (self) are not allowed
}

§No Generic Parameters

Generic parameters are not supported in the implementation either:

trait MyIf {
    fn foo<T>(x: T);
}

struct MyImpl;

#[impl_interface]
impl MyIf for MyImpl {
    fn foo<T>(x: T) {} // error: generic parameters are not allowed
}