Builder

Struct Builder 

Source
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,

Source

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.

Source

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>

Source

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,).

Source

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,).

Trait Implementations§

Source§

impl<T, Dep> Debug for Builder<'_, T, Dep>

Source§

fn fmt(&self, fmt: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'reg, T, Deps> Freeze for Builder<'reg, T, Deps>

§

impl<'reg, T, Deps> !RefUnwindSafe for Builder<'reg, T, Deps>

§

impl<'reg, T, Deps> Send for Builder<'reg, T, Deps>
where T: Send, Deps: Send,

§

impl<'reg, T, Deps> Sync for Builder<'reg, T, Deps>
where T: Sync, Deps: Sync,

§

impl<'reg, T, Deps> Unpin for Builder<'reg, T, Deps>
where T: Unpin, Deps: Unpin,

§

impl<'reg, T, Deps> !UnwindSafe for Builder<'reg, T, Deps>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> Registerable for T
where T: 'static,

Source§

impl<T> RegisterableSingleton for T
where T: Send + Sync + 'static,