Skip to main content

OptionsBuilder

Struct OptionsBuilder 

Source
pub struct OptionsBuilder<'a, T: 'static> { /* private fields */ }
Available on crate feature di only.
Expand description

Represents a builder used to configure Options.

Implementations§

Source§

impl<'a, T: 'static> OptionsBuilder<'a, T>

Source

pub fn new(services: &'a mut ServiceCollection, name: Option<&str>) -> Self

Initializes a new options builder.

§Arguments
Source

pub fn name(&self) -> Option<&str>

Gets the name of the options

Source

pub fn services(&mut self) -> &mut ServiceCollection

Gets the associated collection of services

Source§

impl<'a, T: 'static> OptionsBuilder<'a, T>

Source

pub fn configure<F>(self, setup: F) -> Self
where F: Fn(&mut T) + 'static,

Registers an action used to configure a particular type of Options.

§Arguments
  • setup - The configuration action
Source

pub fn configure1<F, D>(self, setup: F) -> Self
where F: Fn(&mut T, Svc<D>) + 'static, D: 'static,

Registers an action used to configure a particular type of Options with a single dependency.

§Arguments
  • setup - The configuration action
Source

pub fn configure2<F, D1, D2>(self, setup: F) -> Self
where F: Fn(&mut T, Svc<D1>, Svc<D2>) + 'static, D1: 'static, D2: 'static,

Registers an action used to configure a particular type of Options with two dependencies.

§Arguments
  • setup - The configuration action
Source

pub fn configure3<F, D1, D2, D3>(self, setup: F) -> Self
where F: Fn(&mut T, Svc<D1>, Svc<D2>, Svc<D3>) + 'static, D1: 'static, D2: 'static, D3: 'static,

Registers an action used to configure a particular type of Options with three dependencies.

§Arguments
  • setup - The configuration action
Source

pub fn configure4<F, D1, D2, D3, D4>(self, setup: F) -> Self
where F: Fn(&mut T, Svc<D1>, Svc<D2>, Svc<D3>, Svc<D4>) + 'static, D1: 'static, D2: 'static, D3: 'static, D4: 'static,

Registers an action used to configure a particular type of Options with four dependencies.

§Arguments
  • setup - The configuration action
Source

pub fn configure5<F, D1, D2, D3, D4, D5>(self, setup: F) -> Self
where F: Fn(&mut T, Svc<D1>, Svc<D2>, Svc<D3>, Svc<D4>, Svc<D5>) + 'static, D1: 'static, D2: 'static, D3: 'static, D4: 'static, D5: 'static,

Registers an action used to configure a particular type of Options with five dependencies.

§Arguments
  • setup - The configuration action
Source

pub fn post_configure<F>(self, setup: F) -> Self
where F: Fn(&mut T) + 'static,

Registers an action used to configure a particular type of Options.

§Arguments
  • setup - The configuration action
Source

pub fn post_configure1<F, D>(self, setup: F) -> Self
where F: Fn(&mut T, Svc<D>) + 'static, D: 'static,

Registers an action used to configure a particular type of Options with a single dependency.

§Arguments
  • setup - The configuration action
Source

pub fn post_configure2<F, D1, D2>(self, setup: F) -> Self
where F: Fn(&mut T, Svc<D1>, Svc<D2>) + 'static, D1: 'static, D2: 'static,

Registers an action used to configure a particular type of Options with two dependencies.

§Arguments
  • setup - The configuration action
Source

pub fn post_configure3<F, D1, D2, D3>(self, setup: F) -> Self
where F: Fn(&mut T, Svc<D1>, Svc<D2>, Svc<D3>) + 'static, D1: 'static, D2: 'static, D3: 'static,

Registers an action used to configure a particular type of Options with three dependencies.

§Arguments
  • setup - The configuration action
Source

pub fn post_configure4<F, D1, D2, D3, D4>(self, setup: F) -> Self
where F: Fn(&mut T, Svc<D1>, Svc<D2>, Svc<D3>, Svc<D4>) + 'static, D1: 'static, D2: 'static, D3: 'static, D4: 'static,

Registers an action used to configure a particular type of Options with four dependencies.

§Arguments
  • setup - The configuration action
Source

pub fn post_configure5<F, D1, D2, D3, D4, D5>(self, setup: F) -> Self
where F: Fn(&mut T, Svc<D1>, Svc<D2>, Svc<D3>, Svc<D4>, Svc<D5>) + 'static, D1: 'static, D2: 'static, D3: 'static, D4: 'static, D5: 'static,

Registers an action used to configure a particular type of Options with five dependencies.

§Arguments
  • setup - The configuration action
Source

pub fn validate<F, M>(self, action: F, failure_message: M) -> Self
where F: Fn(&T) -> bool + 'static, M: AsRef<str>,

Registers an action used to validate a particular type of Options.

§Arguments
  • action - The validation action
  • failure_message - The message used when validation fails
Source

pub fn validate1<F, M, D>(self, action: F, failure_message: M) -> Self
where F: Fn(&T, Svc<D>) -> bool + 'static, M: AsRef<str>, D: 'static,

Registers an action used to validate a particular type of Options with a single dependency.

§Arguments
  • action - The validation action
  • failure_message - The message used when validation fails
Source

pub fn validate2<F, M, D1, D2>(self, action: F, failure_message: M) -> Self
where F: Fn(&T, Svc<D1>, Svc<D2>) -> bool + 'static, M: AsRef<str>, D1: 'static, D2: 'static,

Registers an action used to validate a particular type of Options with two dependencies.

§Arguments
  • action - The validation action
  • failure_message - The message used when validation fails
Source

pub fn validate3<F, M, D1, D2, D3>(self, action: F, failure_message: M) -> Self
where F: Fn(&T, Svc<D1>, Svc<D2>, Svc<D3>) -> bool + 'static, M: AsRef<str>, D1: 'static, D2: 'static, D3: 'static,

Registers an action used to validate a particular type of Options with three dependencies.

§Arguments
  • action - The validation action
  • failure_message - The message used when validation fails
Source

pub fn validate4<F, M, D1, D2, D3, D4>( self, action: F, failure_message: M, ) -> Self
where F: Fn(&T, Svc<D1>, Svc<D2>, Svc<D3>, Svc<D4>) -> bool + 'static, M: AsRef<str>, D1: 'static, D2: 'static, D3: 'static, D4: 'static,

Registers an action used to validate a particular type of Options with four dependencies.

§Arguments
  • action - The validation action
  • failure_message - The message used when validation fails
Source

pub fn validate5<F, M, D1, D2, D3, D4, D5>( self, action: F, failure_message: M, ) -> Self
where F: Fn(&T, Svc<D1>, Svc<D2>, Svc<D3>, Svc<D4>, Svc<D5>) -> bool + 'static, M: AsRef<str>, D1: 'static, D2: 'static, D3: 'static, D4: 'static, D5: 'static,

Registers an action used to validate a particular type of Options with five dependencies.

§Arguments
  • action - The validation action
  • failure_message - The message used when validation fails

Methods from Deref<Target = ServiceCollection>§

Source

pub fn is_empty(&self) -> bool

Returns true if the collection contains no elements.

Source

pub fn len(&self) -> usize

Returns the number of elements in the collection.

Source

pub fn clear(&mut self)

Removes all elements from the collection.

Source

pub fn remove(&mut self, index: usize) -> ServiceDescriptor

Removes and returns the element at position index within the collection.

§Argument
  • index - The index of the element to remove
§Panics

Panics if index is out of bounds.

Source

pub fn add<T>(&mut self, descriptor: T) -> &mut ServiceCollection

Adds a service using the specified service descriptor.

§Arguments
Source

pub fn try_add<T>(&mut self, descriptor: T) -> &mut ServiceCollection

Adds a service using the specified service descriptor if the service has not already been registered.

§Arguments
Source

pub fn try_add_to_all<T>(&mut self, descriptor: T) -> &mut ServiceCollection

Adds a service using the specified service descriptor if the service with same service and implementation type has not already been registered.

§Arguments
Source

pub fn try_add_all( &mut self, descriptors: impl IntoIterator<Item = ServiceDescriptor>, ) -> &mut ServiceCollection

Adds the specified service descriptors if each of the services are not already registered with the same service and implementation type.

§Arguments
Source

pub fn replace<T>(&mut self, descriptor: T) -> &mut ServiceCollection

Removes the first service descriptor with the same service type and adds the replacement.

§Arguments
Source

pub fn try_replace<T>(&mut self, descriptor: T) -> &mut ServiceCollection

Adds or replaces a service with the specified descriptor if the service has not already been registered.

§Arguments
Source

pub fn remove_all<T>(&mut self) -> &mut ServiceCollection
where T: Any + ?Sized,

Removes all specified descriptors of the specified type.

Source

pub fn build_provider(&self) -> Result<ServiceProvider, ValidationError>

Builds and returns a new ServiceProvider.

Source

pub fn iter(&self) -> impl ExactSizeIterator + DoubleEndedIterator

Gets a read-only iterator for the collection

Source

pub fn decorate<TSvc, TImpl>( &mut self, activate: impl Fn(&ServiceProvider, Rc<TSvc>) -> Rc<TSvc> + 'static, ) -> &mut ServiceCollection
where TSvc: Any + ?Sized,

Decorates an existing service descriptor with a new one that wraps the original.

§Arguments
  • activate - The function that will be called to decorate the resolved service instance
§Remarks

This function will only decorate the last registered ServiceDescriptor for the specified service type. If there are multiple, the others are ignored. If you need to decorate all services of a particular service type, consider using decorate_all instead. If the service to be decorated is not registered, this function does nothing. The decorator ServiceDescriptor is created with the same lifetime as the original service registration. The implementation type of the decorator is determined by the generic parameter TImpl. If the original and decorator implementation types are the same, the original, decorated ServiceDescriptor is not replaced to prevent infinite recursion.

§Example
use di::{injectable, Injectable, ServiceCollection, Ref};

trait Counter {
    fn count(&self) -> usize;
}

#[injectable(Counter)]
struct SingleCount;

impl Counter for SingleCount {
    fn count(&self) -> usize {
        1
    }
}

struct DoubleCount(Ref<dyn Counter>);

impl Counter for DoubleCount {
    fn count(&self) -> usize {
        self.0.count() * 2
    }
}

let provider = ServiceCollection::new()
    .add(SingleCount::transient())
    .decorate::<dyn Counter, DoubleCount>(|_, decorated| Ref::new(DoubleCount(decorated)))
    .build_provider()
    .unwrap();
let counter = provider.get_required::<dyn Counter>();

assert_eq!(counter.count(), 2);
Source

pub fn decorate_all<TSvc, TImpl>( &mut self, activate: impl Fn(&ServiceProvider, Rc<TSvc>) -> Rc<TSvc> + 'static, ) -> &mut ServiceCollection
where TSvc: Any + ?Sized,

Decorates all existing service descriptors with a new one that wraps the original.

§Arguments
  • activate - The function that will be called to decorate the resolved service instance
§Remarks

This function decorates all registered ServiceDescriptor instances for the specified service type. If there are none, this function does nothing. The decorator ServiceDescriptor is created with the same lifetime as the original. If the original, decorated ServiceDescriptor is the same the decorator type, it is ignored.

§Example
use di::{injectable, Injectable, ServiceCollection, Ref};
use std::sync::atomic::{AtomicUsize, Ordering};

trait Feature {
    fn show(&self);
}

#[injectable(Feature)]
struct Feature1;

impl Feature for Feature1 {
    fn show(&self) {
    }
}

#[injectable(Feature)]
struct Feature2;

impl Feature for Feature2 {
    fn show(&self) {
    }
}

#[injectable]
struct Tracker(AtomicUsize);

impl Tracker {
    fn track(&self) {
        self.0.fetch_add(1, Ordering::Relaxed);
    }

    fn count(&self) -> usize {
        self.0.load(Ordering::Relaxed)
    }
}

struct FeatureTracker {
    feature: Ref<dyn Feature>,
    tracker: Ref<Tracker>,
};

impl Feature for FeatureTracker {
    fn show(&self) {
        self.tracker.track();
        self.feature.show();
    }
}

let provider = ServiceCollection::new()
    .add(Tracker::singleton())
    .try_add_to_all(Feature1::transient())
    .try_add_to_all(Feature2::transient())
    .decorate_all::<dyn Feature, FeatureTracker>(|sp, decorated| {
        Ref::new(FeatureTracker { feature: decorated, tracker: sp.get_required::<Tracker>() })
    })
    .build_provider()
    .unwrap();
let features = provider.get_all::<dyn Feature>();
let tracker = provider.get_required::<Tracker>();

for feature in features {
    feature.show();
}

assert_eq!(tracker.count(), 2);

Trait Implementations§

Source§

impl<'a, T> Deref for OptionsBuilder<'a, T>

Source§

type Target = ServiceCollection

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl<'a, T> DerefMut for OptionsBuilder<'a, T>

Source§

fn deref_mut(&mut self) -> &mut Self::Target

Mutably dereferences the value.
Source§

impl<'a, T> From<OptionsBuilder<'a, T>> for &'a mut ServiceCollection

Source§

fn from(builder: OptionsBuilder<'a, T>) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

§

impl<'a, T> Freeze for OptionsBuilder<'a, T>

§

impl<'a, T> !RefUnwindSafe for OptionsBuilder<'a, T>

§

impl<'a, T> !Send for OptionsBuilder<'a, T>

§

impl<'a, T> !Sync for OptionsBuilder<'a, T>

§

impl<'a, T> Unpin for OptionsBuilder<'a, T>
where T: Unpin,

§

impl<'a, T> UnsafeUnpin for OptionsBuilder<'a, T>

§

impl<'a, T> !UnwindSafe for OptionsBuilder<'a, T>

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<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
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> Value for T