Skip to main content

def_interface

Attribute Macro def_interface 

Source
#[def_interface]
Expand description

Define a crate interface.

This attribute should be added above the definition of a trait. All traits that use the attribute cannot have the same name, unless they are assigned different namespaces with namespace = "..." option.

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

See the crate-level documentation for more details.

§Calling Helper Functions

It is also possible to generate calling helper functions for each interface function by enabling the gen_caller option.

§Restrictions

§No Receivers

Methods with receivers (self, &self, &mut self) are not allowed. Only associated functions (static methods) are supported:

#[def_interface]
trait MyIf {
    fn foo(&self); // error: methods with receiver (self) are not allowed
}

§No Generic Parameters

Generic parameters are not supported. Interface functions cannot have generic type parameters, lifetime parameters, or const generic parameters:

#[def_interface]
trait MyIf {
    fn foo<T>(x: T); // error: generic parameters are not allowed
}