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);