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>
impl<TS: Strategy + 'static, T: Identifyable<TS::Id> + Clone + Send + Sync> ServiceProviderFactory<T, TS>
pub fn create( collection: GenericServiceCollection<TS>, parents: RVec<WeakServiceProvider<TS>>, ) -> Result<Self, BuildError<TS>>
Sourcepub fn build(&self, remaining: T) -> ServiceProvider<TS>
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>
impl<T, TS> Sync for ServiceProviderFactory<T, TS>
impl<T, TS> Unpin for ServiceProviderFactory<T, TS>where
T: Unpin,
impl<T, TS> UnwindSafe for ServiceProviderFactory<T, TS>
Blanket Implementations§
Source§impl<T> AlignerFor<1> for T
impl<T> AlignerFor<1> for T
Source§impl<T> AlignerFor<1024> for T
impl<T> AlignerFor<1024> for T
Source§type Aligner = AlignTo1024<T>
type Aligner = AlignTo1024<T>
The
AlignTo*
type which aligns Self
to ALIGNMENT
.Source§impl<T> AlignerFor<128> for T
impl<T> AlignerFor<128> for T
Source§type Aligner = AlignTo128<T>
type Aligner = AlignTo128<T>
The
AlignTo*
type which aligns Self
to ALIGNMENT
.Source§impl<T> AlignerFor<16> for T
impl<T> AlignerFor<16> for T
Source§impl<T> AlignerFor<16384> for T
impl<T> AlignerFor<16384> for T
Source§type Aligner = AlignTo16384<T>
type Aligner = AlignTo16384<T>
The
AlignTo*
type which aligns Self
to ALIGNMENT
.Source§impl<T> AlignerFor<2> for T
impl<T> AlignerFor<2> for T
Source§impl<T> AlignerFor<2048> for T
impl<T> AlignerFor<2048> for T
Source§type Aligner = AlignTo2048<T>
type Aligner = AlignTo2048<T>
The
AlignTo*
type which aligns Self
to ALIGNMENT
.Source§impl<T> AlignerFor<256> for T
impl<T> AlignerFor<256> for T
Source§type Aligner = AlignTo256<T>
type Aligner = AlignTo256<T>
The
AlignTo*
type which aligns Self
to ALIGNMENT
.Source§impl<T> AlignerFor<32> for T
impl<T> AlignerFor<32> for T
Source§impl<T> AlignerFor<32768> for T
impl<T> AlignerFor<32768> for T
Source§type Aligner = AlignTo32768<T>
type Aligner = AlignTo32768<T>
The
AlignTo*
type which aligns Self
to ALIGNMENT
.Source§impl<T> AlignerFor<4> for T
impl<T> AlignerFor<4> for T
Source§impl<T> AlignerFor<4096> for T
impl<T> AlignerFor<4096> for T
Source§type Aligner = AlignTo4096<T>
type Aligner = AlignTo4096<T>
The
AlignTo*
type which aligns Self
to ALIGNMENT
.Source§impl<T> AlignerFor<512> for T
impl<T> AlignerFor<512> for T
Source§type Aligner = AlignTo512<T>
type Aligner = AlignTo512<T>
The
AlignTo*
type which aligns Self
to ALIGNMENT
.Source§impl<T> AlignerFor<64> for T
impl<T> AlignerFor<64> for T
Source§impl<T> AlignerFor<8> for T
impl<T> AlignerFor<8> for T
Source§impl<T> AlignerFor<8192> for T
impl<T> AlignerFor<8192> for T
Source§type Aligner = AlignTo8192<T>
type Aligner = AlignTo8192<T>
The
AlignTo*
type which aligns Self
to ALIGNMENT
.Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<S> ROExtAcc for S
impl<S> ROExtAcc for S
Source§fn f_get<F>(&self, offset: FieldOffset<S, F, Aligned>) -> &F
fn f_get<F>(&self, offset: FieldOffset<S, F, Aligned>) -> &F
Gets a reference to a field, determined by
offset
. Read moreSource§fn f_get_mut<F>(&mut self, offset: FieldOffset<S, F, Aligned>) -> &mut F
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 moreSource§fn f_get_ptr<F, A>(&self, offset: FieldOffset<S, F, A>) -> *const F
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 moreSource§fn f_get_mut_ptr<F, A>(&mut self, offset: FieldOffset<S, F, A>) -> *mut F
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 moreSource§impl<S> ROExtOps<Aligned> for S
impl<S> ROExtOps<Aligned> for S
Source§fn f_replace<F>(&mut self, offset: FieldOffset<S, F, Aligned>, value: F) -> F
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 moreSource§fn f_get_copy<F>(&self, offset: FieldOffset<S, F, Aligned>) -> Fwhere
F: Copy,
fn f_get_copy<F>(&self, offset: FieldOffset<S, F, Aligned>) -> Fwhere
F: Copy,
Source§impl<S> ROExtOps<Unaligned> for S
impl<S> ROExtOps<Unaligned> for S
Source§fn f_replace<F>(&mut self, offset: FieldOffset<S, F, Unaligned>, value: F) -> F
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 moreSource§fn f_get_copy<F>(&self, offset: FieldOffset<S, F, Unaligned>) -> Fwhere
F: Copy,
fn f_get_copy<F>(&self, offset: FieldOffset<S, F, Unaligned>) -> Fwhere
F: Copy,
Source§impl<T> SelfOps for Twhere
T: ?Sized,
impl<T> SelfOps for Twhere
T: ?Sized,
Source§fn piped<F, U>(self, f: F) -> U
fn piped<F, U>(self, f: F) -> U
Emulates the pipeline operator, allowing method syntax in more places. Read more
Source§fn piped_ref<'a, F, U>(&'a self, f: F) -> Uwhere
F: FnOnce(&'a Self) -> U,
fn piped_ref<'a, F, U>(&'a self, f: F) -> Uwhere
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 moreSource§fn piped_mut<'a, F, U>(&'a mut self, f: F) -> Uwhere
F: FnOnce(&'a mut Self) -> U,
fn piped_mut<'a, F, U>(&'a mut self, f: F) -> Uwhere
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
fn mutated<F>(self, f: F) -> Self
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
fn observe<F>(self, f: F) -> Self
Observes the value of self, passing it along unmodified.
Useful in long method chains. Read more
Source§fn as_ref_<T>(&self) -> &T
fn as_ref_<T>(&self) -> &T
Performs a reference to reference conversion with
AsRef
,
using the turbofish .as_ref_::<_>()
syntax. Read moreSource§impl<This> TransmuteElement for Thiswhere
This: ?Sized,
impl<This> TransmuteElement for Thiswhere
This: ?Sized,
Source§unsafe fn transmute_element<T>(self) -> Self::TransmutedPtrwhere
Self: CanTransmuteElement<T>,
unsafe fn transmute_element<T>(self) -> Self::TransmutedPtrwhere
Self: CanTransmuteElement<T>,
Transmutes the element type of this pointer.. Read more
Source§impl<T> TypeIdentity for Twhere
T: ?Sized,
impl<T> TypeIdentity for Twhere
T: ?Sized,
Source§fn as_type_mut(&mut self) -> &mut Self::Type
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>
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>
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>
fn into_type_rc(this: Rc<Self>) -> Rc<Self::Type>
Converts an Rc back to the original type. Read more
Source§fn from_type_ref(this: &Self::Type) -> &Self
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
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>
fn from_type_box(this: Box<Self::Type>) -> Box<Self>
Converts a box back to the original type.