Macro runtime_injector_actix::interface[][src]

macro_rules! interface {
    {
        $(
            $interface:ty = [
                $($(#[$($attr:meta),*])* $impl:ty),*
                $(,)?
            ]
        ),*
        $(,)?
    } => { ... };
}
Expand description

Marks a trait as being an interface for many other types. This means that a request for the given trait can resolve to any of the types indicated by this macro invocation.

With the “arc” feature enabled, the trait must be a subtrait of Send and Sync. This is necessary to allow the service pointers to be downcasted. If the “rc” feature is enabled, this is not required. Additionally, instances of the trait must have a 'static lifetime. This can be done easily by making your interface a subtrait of Service.

Example

use runtime_injector::{interface, Service};

struct Bar;
#[cfg(test)]
struct MockBar;

trait Foo: Service {}
impl Foo for Bar {}
#[cfg(test)]
impl Foo for MockBar {}

// Requests for `dyn Foo` can resolve to either `Bar` or, in a test run,
// `MockBar`. Note that attributes are allowed on each of the listed types.
interface! {
    dyn Foo = [
        Bar,
        #[cfg(test)]
        MockBar,
    ],
};