Provider

Trait Provider 

Source
pub trait Provider<'cx, S: Service>:
    Send
    + Sync
    + 'cx {
    // Required method
    fn provide(
        &'cx self,
        cx: &'cx Context<'_>,
        arg: S::Argument<'_>,
    ) -> S::Output<'cx>;
}
Expand description

A can construct a Service which references objects either inside itself or the provided Context.

Any type implementing the appropriate Fn trait can be used as a provider:

// A service providing a random number
#[derive(Service)]
struct Random(u64);

// Create a context and bind Random to a provider
let mut cx = Context::new();
cx.bind_fn::<Random>(|_cx, _arg| Random(rand::random()));

// Print a random number
println!("{}", cx.resolve::<Random>().0);

Required Methods§

Source

fn provide( &'cx self, cx: &'cx Context<'_>, arg: S::Argument<'_>, ) -> S::Output<'cx>

Build the output object

Implementors§

Source§

impl<'cx, F, S> Provider<'cx, S> for F
where F: Fn(&'cx Context<'_>, S::Argument<'_>) -> S::Output<'cx> + Send + Sync + 'cx, S: Service,

Allow Fn functions to act as providers.