ferrunix_core::registry

Struct Registry

source
pub struct Registry { /* private fields */ }
Expand description

Registry for all types that can be constructed or otherwise injected.

Implementations§

source§

impl Registry

source

pub fn empty() -> Self

Create a new, empty, registry. This registry contains no pre-registered types.

Types that are auto-registered are also not included in this registry.

To get access to the auto-registered types (types that are annotated by the derive macro), the global registry Registry::global needs to be used.

source

pub fn autoregistered() -> Self

Create an empty registry, and add all autoregistered types into it.

This is the constructor for the global registry that can be acquired with Registry::global.

source

pub fn transient<T>(&self, ctor: fn() -> T)
where T: Registerable,

Register a new transient object, without dependencies.

To register a type with dependencies, use the builder returned from Registry::with_deps.

§Parameters
  • ctor: A constructor function returning the newly constructed T. This constructor will be called for every T that is requested.
source

pub fn singleton<T>(&self, ctor: fn() -> T)
where T: Registerable,

Register a new singleton object, without dependencies.

To register a type with dependencies, use the builder returned from Registry::with_deps.

§Parameters
  • ctor: A constructor function returning the newly constructed T. This constructor will be called once, lazily, when the first instance of T is requested.
source

pub fn get_transient<T>(&self) -> Option<T>
where T: Registerable,

Retrieves a newly constructed T from this registry.

Returns None if T wasn’t registered or failed to construct.

source

pub fn get_singleton<T>(&self) -> Option<Ref<T>>
where T: Registerable,

Retrieves the singleton T from this registry.

Returns None if T wasn’t registered or failed to construct. The singleton is a ref-counted pointer object (either Arc or Rc).

source

pub fn with_deps<T, Deps>(&self) -> Builder<'_, T, Deps>
where Deps: DepBuilder<T>,

Register a new transient or singleton with dependencies.

source

pub fn validate_all(&self) -> bool

Check whether all registered types have the required dependencies.

Returns true if for all registered types all of it’s dependencies can be constructed, false otherwise.

This is a potentially expensive call since it needs to go through the entire dependency tree for each registered type.

Nontheless, it’s recommended to call this before using the Registry.

source

pub fn validate<T>(&self) -> bool
where T: Registerable,

Check whether the type T is registered in this registry, and all dependencies of the type T are also registered.

Returns true if the type and it’s dependencies can be constructed, false otherwise.

source

pub fn global() -> &'static Self

Access the global registry.

This registry contains the types that are marked for auto-registration via the derive macro.

source

pub unsafe fn reset_global()

Reset the global registry, removing all previously registered types, and re-running the auto-registration routines.

§Safety

Ensure that no other thread is currently using Registry::global().

Trait Implementations§

source§

impl Debug for Registry

source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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.