Trait FromContext

Source
pub trait FromContext: Sized {
    // Required method
    fn from_context(
        context: &mut ServiceContext<'_>,
    ) -> Result<Self, WiringError>;
}
Expand description

Trait used as input for wiring layers, aiming to provide all the resources the layer needs for wiring.

For most cases, the most conevenient way to implement this trait is to use the #[derive(FromContext)]. Otherwise, the trait has several blanket implementations (including the implementation for () and Option).

§Example

use wire_framework::FromContext;
#[derive(FromContext)]
struct MyWiringLayerInput {
    // The following field _must_ be present in the context.
    mandatory_resource: MandatoryResource,
    // The following field is optional.
    // If will be `None` if there is no such resource in the context.
    optional_resource: Option<OptionalResource>,
    // The following field is guaranteed to fetch the value from the context.
    // In case the value is missing, a default value will be added to the context.
    #[context(default)]
    resource_with_default: ResourceWithDefault,
}

Required Methods§

Source

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

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl FromContext for ()

Source§

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

Source§

impl<T: FromContext> FromContext for Option<T>

Source§

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

Implementors§