Skip to main content

ProviderSpec

Derive Macro ProviderSpec 

Source
#[derive(ProviderSpec)]
{
    // Attributes available to this derive:
    #[provides]
    #[named]
}
Expand description

Derive an inherent layer() (an id_effect::Layer providing the service) from #[provides(Service)]. Re-exported by id_effect as ProviderSpecDerive.

Struct::layer::<E>() is a Layer<HCons<Service, HNil>, E> that needs nothing and never fails. The service value comes from, in order of precedence:

  • #[provides(Service, with = <constructor>)] — || expr, or a path such as Self::build called with no arguments. This is the recommended form: no fn new() -> Service (which trips clippy::new_ret_no_self) is needed.
  • Self::default() when the struct derives Default (the provider is its own service).
  • Self::new() otherwise.

#[provides(Service, release = <finalizer>)] makes it an acquire_release layer whose finalizer release(Arc<Service>) (an Effect<(), Never, HNil>) runs when the layer’s scope closes. The 0.6 forms |deps: &Env| .. and #[named("..")] are compile errors.

ⓘ
#[derive(Clone)]
struct Counter(u32);

#[derive(ProviderSpecDerive)]
#[provides(Counter, with = || Counter(42))]
struct CounterLive;

let program = app().provide_layer(CounterLive::layer());