pub struct Builder<'reg, 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: DepBuilder<T> + 'static> Builder<'_, T, Deps>where
T: Registerable,
impl<T, Deps: DepBuilder<T> + 'static> Builder<'_, T, Deps>where
T: Registerable,
Sourcepub fn register_transient<C>(&self, ctor: C)where
C: TransientCtorDeps<T, Deps> + Copy + 'static,
pub fn register_transient<C>(&self, ctor: C)where
C: TransientCtorDeps<T, Deps> + Copy + 'static,
Register a new transient object, with dependencies specified in Registry::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>)>()
.register_transient(|(num, template)| {
// access `num` and `template` here.
u16::from(*num)
});For single dependencies, the destructured tuple needs to end with a
comma: (dep,).
§Panics
When the type has been registered already.
Sourcepub fn try_register_transient<C>(&self, ctor: C)where
C: TransientCtorFallibleDeps<T, Deps> + Copy + 'static,
pub fn try_register_transient<C>(&self, ctor: C)where
C: TransientCtorFallibleDeps<T, Deps> + Copy + 'static,
Register a new transient object, with dependencies specified in Registry::with_deps.
The ctor parameter is a constructor function returning a Result<T, E>. 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>)>()
.register_transient(|(num, template)| {
// access `num` and `template` here.
u16::from(*num)
});For single dependencies, the destructured tuple needs to end with a
comma: (dep,).
§Panics
When the type has been registered already.
Source§impl<T, Deps: DepBuilder<T> + 'static> Builder<'_, T, Deps>where
T: RegisterableSingleton,
impl<T, Deps: DepBuilder<T> + 'static> Builder<'_, T, Deps>where
T: RegisterableSingleton,
Sourcepub fn register_singleton<F>(&self, ctor: F)where
F: SingletonCtorDeps<T, Deps>,
pub fn register_singleton<F>(&self, ctor: F)where
F: SingletonCtorDeps<T, Deps>,
Register a new singleton object, with dependencies specified by
Registry::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>)>()
.register_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 try_register_singleton<F>(&self, ctor: F)where
F: SingletonCtorFallibleDeps<T, Deps>,
pub fn try_register_singleton<F>(&self, ctor: F)where
F: SingletonCtorFallibleDeps<T, Deps>,
Register a new singleton object, with dependencies specified by
Registry::with_deps.
The ctor parameter is a constructor function returning a Result<T, E>. 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>)>()
.register_transient(|(num, template)| {
// access `num` and `template` here.
u16::from(*num)
});For single dependencies, the destructured tuple needs to end with a comma: (dep,).