ferrunix_core::registry

Struct Builder

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

source

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

source

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

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<'a, T, Deps> Freeze for Builder<'a, T, Deps>

§

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

§

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

§

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

§

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

§

impl<'a, T, Deps> !UnwindSafe for Builder<'a, 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.