pub struct Builder<'a, T, Deps> { /* private fields */ }Expand description
A builder for objects with dependencies. This can be created by using
Registry::with_deps.
Implementations§
source§impl<T, Deps> Builder<'_, T, Deps>where
Deps: DepBuilder<T> + 'static,
T: Registerable,
impl<T, Deps> Builder<'_, T, Deps>where
Deps: DepBuilder<T> + 'static,
T: Registerable,
sourcepub fn transient(&self, ctor: fn(_: Deps) -> T)
pub fn transient(&self, ctor: fn(_: Deps) -> T)
Register a new transient object, with dependencies specified in
.with_deps.
The ctor parameter is a constructor function returning the newly
constructed T. The constructor accepts a single argument Deps (a
tuple implementing crate::dependency_builder::DepBuilder). It’s
best to destructure the tuple to accept each dependency separately.
This constructor will be called for every T that is requested.
§Example
registry
.with_deps::<_, (Transient<u8>, Singleton<Template>)>()
.transient(|(num, template)| {
// access `num` and `template` here.
u16::from(*num)
});For single dependencies, the destructured tuple needs to end with a
comma: (dep,).
sourcepub fn singleton(&self, ctor: fn(_: Deps) -> T)
pub fn singleton(&self, ctor: fn(_: Deps) -> T)
Register a new singleton object, with dependencies specified in
.with_deps.
The ctor parameter is a constructor function returning the newly
constructed T. The constructor accepts a single argument Deps (a
tuple implementing crate::dependency_builder::DepBuilder). It’s
best to destructure the tuple to accept each dependency separately.
This constructor will be called once, lazily, when the first
instance of T is requested.
§Example
registry
.with_deps::<_, (Transient<u8>, Singleton<Template>)>()
.transient(|(num, template)| {
// access `num` and `template` here.
u16::from(*num)
});For single dependencies, the destructured tuple needs to end with a
comma: (dep,).