#[entrait]
Expand description
The entrait attribute macro, used to generate traits and implementations of them.
For functions
When used with a function, the macro must be given the name of a trait to generate. The macro will generate that trait, and connect it to the function by supplying an implementation for Impl, plus optional mock implementations.
Syntax
#[entrait($visibility? $TraitIdent)]
fn ...
$visibility
: Optional visibility specifier for the generated trait. See the Rust documentation for valid values.$TraitIdent
: Any valid Rust identifier that starts with an upper-case character, used as the name of the new trait.
with options:
#[entrait($visibility? $TraitIdent, $option, ...)]
fn ...
For traits
When used with a trait, the macro will only supply an implementation for Impl plus optional mock implementations.
As there is no implementation function to connect the trait to, the job of actual implementation is left to user code.
The generated Impl-implementation works by delegating: The trait will only be implemented for Impl<T>
when it is also implemented for T
.
User code has to supply a manual implementation of the trait for the T
that is going to be used in the application.
Inside this implementation, the application is no longer generic, therefore this will become a leaf dependency.
Mock exporting is implicitly turned on. The reason is that the only reasonable use case of entraiting a trait is that the trait has to live in an upstream crate, without access to the downstream application type.
Syntax
#[entrait]
trait ...
with options:
#[entrait($option, ...)]
trait ...
Options
An option can be just $option
or $option = $value
. An option without value means true
.
Option | Type | Target | Default | Description |
---|---|---|---|---|
no_deps | bool | fn | false | Disables the dependency parameter, so that the first parameter is just interpreted as a normal function parameter. Useful for reducing noise in some situations. |
export | bool | fn | false | If mocks are generated, exports these mocks even in release builds. Only relevant for libraries. |
unimock | bool | fn+trait | false 1 | Used to turn off unimock implementation when the unimock feature is enabled. |
mockall | bool | fn+trait | false | Enable mockall mocks. |
async_trait | bool | fn | false 2 | In the case of an async fn , use the async_trait macro on the resulting trait. Requires the async_trait entrait feature. |
associated_future | bool | fn | false 3 | In the case of an async fn , use an associated future to avoid heap allocation. Currently requires a nighlty Rust compiler, with feature(generic_associated_types) and feature(type_alias_impl_trait) . |