Struct ServiceProviderFactory

Source
pub struct ServiceProviderFactory<T: Clone + Send + Sync, TS: Strategy + 'static = AnyStrategy> { /* private fields */ }
Expand description

Performs all checks to build a ServiceProvider on premise that an instance of type T will be available. Therefore, multiple ServiceProvider with a different base can be created very efficiently. This base could e.g. be the ApplicationSettings for the DomainServices or the HttpContext, if one ServiceProvider is generated per HTTP-Request in a WebApi

use {minfac::{Registered, ServiceCollection}};

let mut collection = ServiceCollection::new();
collection.with::<Registered<i32>>().register(|v| v as i64);
let factory = collection.build_factory().expect("Config should be valid");
let provider1 = factory.build(1);
let provider2 = factory.build(2);

assert_eq!(Some(1i64), provider1.get::<i64>());
assert_eq!(Some(2i64), provider2.get::<i64>());

Implementations§

Source§

impl<TS: Strategy + 'static, T: Identifyable<TS::Id> + Clone + Send + Sync> ServiceProviderFactory<T, TS>

Source

pub fn create( collection: GenericServiceCollection<TS>, parents: RVec<WeakServiceProvider<TS>>, ) -> Result<Self, BuildError<TS>>

Source

pub fn build(&self, remaining: T) -> ServiceProvider<TS>

The ServiceProvider should always be assigned to a variable. Otherwise, a requested shared service it will outlive its ServiceProvider, resulting in a panic if debug_assertions are enabled

use {minfac::{Registered, ServiceCollection}, std::sync::Arc};
let result = std::panic::catch_unwind(|| {
    let mut collection = ServiceCollection::new();
    collection.register_shared(|| Arc::new(42));
    let factory = collection.build_factory().expect("Configuration is valid");
    let x = factory.build(1).get::<Arc<i32>>(); // ServiceProvider is dropped too early
});
assert!(result.is_err());
Examples found in repository?
examples/complete.rs (line 24)
9fn main() {
10    let mut parent_collection = ServiceCollection::new();
11    parent_collection
12        .with::<AllRegistered<u8>>()
13        .register(|bytes| bytes.map(|signed| signed as i8).sum::<i8>());
14
15    parent_collection
16        .with::<Registered<Arc<u16>>>()
17        .register(|i| *i as u32 * 2);
18    parent_collection.register(|| 1u8);
19    parent_collection.register_shared(|| Arc::new(10u16));
20
21    let parent_provider = parent_collection
22        .build_factory()
23        .expect("All dependencies of parent should be resolvable")
24        .build(2u8);
25
26    let mut child_collection = ServiceCollection::new();
27    child_collection.register(|| 3u8);
28    child_collection
29        .with::<(WeakServiceProvider, AllRegistered<u8>, Registered<u32>)>()
30        .register(|(provider, bytes, int)| {
31            provider.get::<Arc<u16>>().map(|i| *i as u64).unwrap_or(1000) // Optional Dependency, fallback not used
32                + provider.get::<u128>().map(|i| i as u64).unwrap_or(2000) // Optional Dependency, fallback
33                + bytes.map(|i| { i as u64 }).sum::<u64>()
34                + int as u64
35        });
36
37    let child_provider = child_collection
38        .with_parent(&parent_provider)
39        .build_factory()
40        .expect("All dependencies of child should be resolvable")
41        .build(4u8);
42
43    assert_eq!(Some(2), parent_provider.get::<u8>()); // Last registered i8 on parent
44    assert_eq!(Some(4), child_provider.get::<u8>()); // Last registered i8 on client
45
46    assert_eq!(
47        Some(10 + 2000 + (1 + 2 + 3 + 4) + (2 * 10)),
48        child_provider.get::<u64>()
49    );
50
51    assert_eq!(
52        1 + 2, // Despite u8 beign received by child_provider, u8 is just the sum of i8 from parent
53        child_provider.get::<i8>().unwrap()
54    );
55    println!("Everything works as expected");
56}

Auto Trait Implementations§

§

impl<T, TS> Freeze for ServiceProviderFactory<T, TS>

§

impl<T, TS> RefUnwindSafe for ServiceProviderFactory<T, TS>
where T: RefUnwindSafe, <TS as Strategy>::Id: RefUnwindSafe,

§

impl<T, TS> Send for ServiceProviderFactory<T, TS>
where <TS as Strategy>::Id: Send + Sync,

§

impl<T, TS> Sync for ServiceProviderFactory<T, TS>
where <TS as Strategy>::Id: Send + Sync,

§

impl<T, TS> Unpin for ServiceProviderFactory<T, TS>
where T: Unpin,

§

impl<T, TS> UnwindSafe for ServiceProviderFactory<T, TS>
where T: UnwindSafe, <TS as Strategy>::Id: RefUnwindSafe + UnwindSafe,

Blanket Implementations§

Source§

impl<T> AlignerFor<1> for T

Source§

type Aligner = AlignTo1<T>

The AlignTo* type which aligns Self to ALIGNMENT.
Source§

impl<T> AlignerFor<1024> for T

Source§

type Aligner = AlignTo1024<T>

The AlignTo* type which aligns Self to ALIGNMENT.
Source§

impl<T> AlignerFor<128> for T

Source§

type Aligner = AlignTo128<T>

The AlignTo* type which aligns Self to ALIGNMENT.
Source§

impl<T> AlignerFor<16> for T

Source§

type Aligner = AlignTo16<T>

The AlignTo* type which aligns Self to ALIGNMENT.
Source§

impl<T> AlignerFor<16384> for T

Source§

type Aligner = AlignTo16384<T>

The AlignTo* type which aligns Self to ALIGNMENT.
Source§

impl<T> AlignerFor<2> for T

Source§

type Aligner = AlignTo2<T>

The AlignTo* type which aligns Self to ALIGNMENT.
Source§

impl<T> AlignerFor<2048> for T

Source§

type Aligner = AlignTo2048<T>

The AlignTo* type which aligns Self to ALIGNMENT.
Source§

impl<T> AlignerFor<256> for T

Source§

type Aligner = AlignTo256<T>

The AlignTo* type which aligns Self to ALIGNMENT.
Source§

impl<T> AlignerFor<32> for T

Source§

type Aligner = AlignTo32<T>

The AlignTo* type which aligns Self to ALIGNMENT.
Source§

impl<T> AlignerFor<32768> for T

Source§

type Aligner = AlignTo32768<T>

The AlignTo* type which aligns Self to ALIGNMENT.
Source§

impl<T> AlignerFor<4> for T

Source§

type Aligner = AlignTo4<T>

The AlignTo* type which aligns Self to ALIGNMENT.
Source§

impl<T> AlignerFor<4096> for T

Source§

type Aligner = AlignTo4096<T>

The AlignTo* type which aligns Self to ALIGNMENT.
Source§

impl<T> AlignerFor<512> for T

Source§

type Aligner = AlignTo512<T>

The AlignTo* type which aligns Self to ALIGNMENT.
Source§

impl<T> AlignerFor<64> for T

Source§

type Aligner = AlignTo64<T>

The AlignTo* type which aligns Self to ALIGNMENT.
Source§

impl<T> AlignerFor<8> for T

Source§

type Aligner = AlignTo8<T>

The AlignTo* type which aligns Self to ALIGNMENT.
Source§

impl<T> AlignerFor<8192> for T

Source§

type Aligner = AlignTo8192<T>

The AlignTo* type which aligns Self to ALIGNMENT.
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<S> ROExtAcc for S

Source§

fn f_get<F>(&self, offset: FieldOffset<S, F, Aligned>) -> &F

Gets a reference to a field, determined by offset. Read more
Source§

fn f_get_mut<F>(&mut self, offset: FieldOffset<S, F, Aligned>) -> &mut F

Gets a muatble reference to a field, determined by offset. Read more
Source§

fn f_get_ptr<F, A>(&self, offset: FieldOffset<S, F, A>) -> *const F

Gets a const pointer to a field, the field is determined by offset. Read more
Source§

fn f_get_mut_ptr<F, A>(&mut self, offset: FieldOffset<S, F, A>) -> *mut F

Gets a mutable pointer to a field, determined by offset. Read more
Source§

impl<S> ROExtOps<Aligned> for S

Source§

fn f_replace<F>(&mut self, offset: FieldOffset<S, F, Aligned>, value: F) -> F

Replaces a field (determined by offset) with value, returning the previous value of the field. Read more
Source§

fn f_swap<F>(&mut self, offset: FieldOffset<S, F, Aligned>, right: &mut S)

Swaps a field (determined by offset) with the same field in right. Read more
Source§

fn f_get_copy<F>(&self, offset: FieldOffset<S, F, Aligned>) -> F
where F: Copy,

Gets a copy of a field (determined by offset). The field is determined by offset. Read more
Source§

impl<S> ROExtOps<Unaligned> for S

Source§

fn f_replace<F>(&mut self, offset: FieldOffset<S, F, Unaligned>, value: F) -> F

Replaces a field (determined by offset) with value, returning the previous value of the field. Read more
Source§

fn f_swap<F>(&mut self, offset: FieldOffset<S, F, Unaligned>, right: &mut S)

Swaps a field (determined by offset) with the same field in right. Read more
Source§

fn f_get_copy<F>(&self, offset: FieldOffset<S, F, Unaligned>) -> F
where F: Copy,

Gets a copy of a field (determined by offset). The field is determined by offset. Read more
Source§

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

Source§

fn eq_id(&self, other: &Self) -> bool

Compares the address of self with the address of other. Read more
Source§

fn piped<F, U>(self, f: F) -> U
where F: FnOnce(Self) -> U, Self: Sized,

Emulates the pipeline operator, allowing method syntax in more places. Read more
Source§

fn piped_ref<'a, F, U>(&'a self, f: F) -> U
where F: FnOnce(&'a Self) -> U,

The same as piped except that the function takes &Self Useful for functions that take &Self instead of Self. Read more
Source§

fn piped_mut<'a, F, U>(&'a mut self, f: F) -> U
where F: FnOnce(&'a mut Self) -> U,

The same as piped, except that the function takes &mut Self. Useful for functions that take &mut Self instead of Self.
Source§

fn mutated<F>(self, f: F) -> Self
where F: FnOnce(&mut Self), Self: Sized,

Mutates self using a closure taking self by mutable reference, passing it along the method chain. Read more
Source§

fn observe<F>(self, f: F) -> Self
where F: FnOnce(&Self), Self: Sized,

Observes the value of self, passing it along unmodified. Useful in long method chains. Read more
Source§

fn into_<T>(self) -> T
where Self: Into<T>,

Performs a conversion with Into. using the turbofish .into_::<_>() syntax. Read more
Source§

fn as_ref_<T>(&self) -> &T
where Self: AsRef<T>, T: ?Sized,

Performs a reference to reference conversion with AsRef, using the turbofish .as_ref_::<_>() syntax. Read more
Source§

fn as_mut_<T>(&mut self) -> &mut T
where Self: AsMut<T>, T: ?Sized,

Performs a mutable reference to mutable reference conversion with AsMut, using the turbofish .as_mut_::<_>() syntax. Read more
Source§

fn drop_(self)
where Self: Sized,

Drops self using method notation. Alternative to std::mem::drop. Read more
Source§

impl<This> TransmuteElement for This
where This: ?Sized,

Source§

unsafe fn transmute_element<T>(self) -> Self::TransmutedPtr
where Self: CanTransmuteElement<T>,

Transmutes the element type of this pointer.. Read more
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> TypeIdentity for T
where T: ?Sized,

Source§

type Type = T

This is always Self.
Source§

fn into_type(self) -> Self::Type
where Self: Sized, Self::Type: Sized,

Converts a value back to the original type.
Source§

fn as_type(&self) -> &Self::Type

Converts a reference back to the original type.
Source§

fn as_type_mut(&mut self) -> &mut Self::Type

Converts a mutable reference back to the original type.
Source§

fn into_type_box(self: Box<Self>) -> Box<Self::Type>

Converts a box back to the original type.
Source§

fn into_type_arc(this: Arc<Self>) -> Arc<Self::Type>

Converts an Arc back to the original type. Read more
Source§

fn into_type_rc(this: Rc<Self>) -> Rc<Self::Type>

Converts an Rc back to the original type. Read more
Source§

fn from_type(this: Self::Type) -> Self
where Self: Sized, Self::Type: Sized,

Converts a value back to the original type.
Source§

fn from_type_ref(this: &Self::Type) -> &Self

Converts a reference back to the original type.
Source§

fn from_type_mut(this: &mut Self::Type) -> &mut Self

Converts a mutable reference back to the original type.
Source§

fn from_type_box(this: Box<Self::Type>) -> Box<Self>

Converts a box back to the original type.
Source§

fn from_type_arc(this: Arc<Self::Type>) -> Arc<Self>

Converts an Arc back to the original type.
Source§

fn from_type_rc(this: Rc<Self::Type>) -> Rc<Self>

Converts an Rc back to the original type.