Trait IntoContext

Source
pub trait IntoContext {
    // Required method
    fn into_context(
        self,
        context: &mut ServiceContext<'_>,
    ) -> Result<(), WiringError>;
}
Expand description

Trait used as output for wiring layers, aiming to provide all the resources and tasks the layer creates.

For most cases, the most conevenient way to implement this trait is to use the #[derive(IntoContext)]. Otherwise, the trait has several blanket implementations (including the implementation for () and Option). Note, however, that due to the lack of specialization, the blanket implementation for Option<T: Task> is not provided. When used in the macro, tasks must be annotated with the #[context(task)] attribute.

Note: returning a resource that already exists in the context will result in a wiring error. If you need to provide a “substitute” resource, request Option of it in the FromContext implementation to check whether it’s already provided.

§Example

use wire_framework::IntoContext;
#[derive(IntoContext)]
struct MyWiringLayerOutput {
    // This resource will be inserted unconditionally.
    // Will err if such resource is already present in the context.
    recource: MyResource,
    // Will only provide the resource if it's `Some`.
    maybe_resource: Option<MaybeResource>,
    // Will provide task unconditionally.
    #[context(task)]
    task: MyTask,
    // Will provide task only if it's `Some`.
    #[context(task)]
    maybe_task: Option<MaybeTask>,
}

Required Methods§

Source

fn into_context( self, context: &mut ServiceContext<'_>, ) -> Result<(), WiringError>

Implementations on Foreign Types§

Source§

impl IntoContext for ()

Source§

fn into_context( self, _context: &mut ServiceContext<'_>, ) -> Result<(), WiringError>

Source§

impl<T: IntoContext> IntoContext for Option<T>

Source§

fn into_context( self, context: &mut ServiceContext<'_>, ) -> Result<(), WiringError>

Implementors§