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 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) -> Result<(), ValidationError>

Check whether all registered types have the required dependencies.

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.

§Errors

Returns a ValidationError when the dependency graph is missing dependencies or has cycles.

Source

pub fn validate_all_full(&self) -> Result<(), FullValidationError>

Check whether all registered types have the required dependencies and returns a detailed error about what’s missing or where a cycle was detected.

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

§Errors

Returns a FullValidationError when the dependency graph is missing dependencies or has cycles.

Source

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

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

§Errors

Returns a ValidationError when the dependency graph is missing dependencies or has cycles.

Source

pub fn dotgraph(&self) -> Result<String, ValidationError>

Return a string of the dependency graph visualized using graphviz’s dot language.

§Errors

Returns a ValidationError when the dependency graph is missing dependencies or has cycles.

Source§

impl Registry

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§

impl Registry

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.
§Panics

When the type has been registered already.

Source

pub fn singleton<T, F>(&self, ctor: F)

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.
§Panics

When the type has been registered already.

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

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

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.

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.
Source§

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

Source§

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