Attribute Macro entrait::entrait

source · []
#[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.

OptionTypeTargetDefaultDescription
no_depsboolfnfalseDisables the dependency parameter, so that the first parameter is just interpreted as a normal function parameter. Useful for reducing noise in some situations.
exportboolfnfalseIf mocks are generated, exports these mocks even in release builds. Only relevant for libraries.
unimockboolfn+traitfalse1Used to turn off unimock implementation when the unimock feature is enabled.
mockallboolfn+traitfalseEnable mockall mocks.
async_traitboolfnfalse2In the case of an async fn, use the async_trait macro on the resulting trait. Requires the async_trait entrait feature.
associated_futureboolfnfalse3In 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).

  1. Enabled by default by turning on the unimock cargo feature. 

  2. Enabled by default by turning on the use-async-trait cargo feature. 

  3. Enabled by default by turning on the use-associated-future cargo feature.