pub struct ContextOptions { /* private fields */ }
Expand description
Options and flags which can be used to configure how a context is created.
This builder expose the ability to configure how a Context
is created.
The Context::create
and Context::auto_register
methods are aliases
for commonly used options using this builder.
Generally speaking, when using ContextOptions
, you’ll first call ContextOptions::default
,
then chain calls to methods to set each option, then call ContextOptions::create
, paasing the modules you’ve built,
or call ContextOptions::auto_register
. This will give you a Context
.
§Example
Creating a context with a module:
use rudi::{modules, Context, ContextOptions, DynProvider, Module};
struct MyModule;
impl Module for MyModule {
fn providers() -> Vec<DynProvider> {
vec![]
}
}
let _cx: Context = ContextOptions::default().create(modules![MyModule]);
Creating a context with AutoRegisterModule
:
use rudi::{modules, AutoRegisterModule, Context, ContextOptions};
let _cx: Context = ContextOptions::default().create(modules![AutoRegisterModule]);
// or use simpler method
// let _cx: Context = ContextOptions::default().auto_register();
Creating a context with both options:
use rudi::{modules, AutoRegisterModule, Context, ContextOptions};
let _cx: Context = ContextOptions::default()
.allow_override(true)
.allow_only_single_eager_create(true)
.eager_create(false)
.singleton(42)
.singleton_with_name("Hello", "str_1")
.singleton_with_name("World", "str_2")
.create(modules![AutoRegisterModule]);
Implementations§
Source§impl ContextOptions
impl ContextOptions
Sourcepub fn allow_override(self, allow_override: bool) -> Self
pub fn allow_override(self, allow_override: bool) -> Self
Sets the option for whether the context should allow overriding existing providers.
This option, when true, allows a provider to override an existing provider with the same key.
§Example
use rudi::{modules, Context, ContextOptions};
let cx: Context = ContextOptions::default()
.allow_override(true)
.create(modules![]);
assert!(cx.allow_override());
Sourcepub fn allow_only_single_eager_create(
self,
allow_only_single_eager_create: bool,
) -> Self
pub fn allow_only_single_eager_create( self, allow_only_single_eager_create: bool, ) -> Self
Sets the option for whether the context should only eagerly create Singleton
and SingleOwner
instances.
This option, when true, will only eagerly create instances for Singleton
and SingleOwner
providers.
§Example
use rudi::{modules, Context, ContextOptions};
let cx: Context = ContextOptions::default()
.allow_only_single_eager_create(false)
.create(modules![]);
assert!(!cx.allow_only_single_eager_create());
Sourcepub fn eager_create(self, eager_create: bool) -> Self
pub fn eager_create(self, eager_create: bool) -> Self
Sets the option for whether the context should eagerly create instances.
This option, when true, will eagerly create instances for all providers.
§Example
use rudi::{modules, Context, ContextOptions};
let cx: Context = ContextOptions::default()
.eager_create(false)
.create(modules![]);
assert!(!cx.eager_create());
Sourcepub fn singleton_with_name<T, N>(self, instance: T, name: N) -> Self
pub fn singleton_with_name<T, N>(self, instance: T, name: N) -> Self
Appends a standalone Singleton
instance to the context with name.
§Example
use rudi::{modules, Context, ContextOptions};
let cx: Context = ContextOptions::default()
.singleton_with_name(1, "one")
.singleton_with_name(2, "two")
.create(modules![]);
assert_eq!(cx.get_single_with_name::<i32>("one"), &1);
assert_eq!(cx.get_single_with_name::<i32>("two"), &2);
Sourcepub fn single_owner<T>(self, instance: T) -> Selfwhere
T: 'static,
pub fn single_owner<T>(self, instance: T) -> Selfwhere
T: 'static,
Appends a standalone SingleOwner
instance to the context with default name ""
.
§Example
use rudi::{modules, Context, ContextOptions};
#[derive(PartialEq, Eq, Debug)]
struct NotClone(i32);
let cx: Context = ContextOptions::default()
.single_owner(NotClone(42))
.create(modules![]);
assert_eq!(cx.get_single::<NotClone>(), &NotClone(42));
Sourcepub fn single_owner_with_name<T, N>(self, instance: T, name: N) -> Self
pub fn single_owner_with_name<T, N>(self, instance: T, name: N) -> Self
Appends a standalone SingleOwner
instance to the context with name.
§Example
use rudi::{modules, Context, ContextOptions};
#[derive(PartialEq, Eq, Debug)]
struct NotClone(i32);
let cx: Context = ContextOptions::default()
.single_owner_with_name(NotClone(1), "one")
.single_owner_with_name(NotClone(2), "two")
.create(modules![]);
assert_eq!(cx.get_single_with_name::<NotClone>("one"), &NotClone(1));
assert_eq!(cx.get_single_with_name::<NotClone>("two"), &NotClone(2));
Sourcepub fn create(self, modules: Vec<ResolveModule>) -> Context
pub fn create(self, modules: Vec<ResolveModule>) -> Context
Creates a new context with the given modules.
§Panics
- Panics if there are multiple providers with the same key and the context’s
allow_override
is false. - Panics if there is a provider whose constructor is async and the provider will be eagerly created.
- Panics if there is a provider that panics on construction.
§Example
use rudi::{components, modules, Context, ContextOptions, DynProvider, Module, Transient};
#[Transient]
struct A;
struct MyModule;
impl Module for MyModule {
fn providers() -> Vec<DynProvider> {
components![A]
}
}
let mut cx: Context = ContextOptions::default().create(modules![MyModule]);
assert!(cx.resolve_option::<A>().is_some());
Sourcepub fn auto_register(self) -> Context
Available on crate feature auto-register
only.
pub fn auto_register(self) -> Context
auto-register
only.Creates a new context with the AutoRegisterModule
.
Same as ContextOptions::default().create(modules![AutoRegisterModule])
.
See ContextOptions::create
for more details.
§Panics
- Panics if there are multiple providers with the same key and the context’s
allow_override
is false. - Panics if there is a provider whose constructor is async and the provider will be eagerly created.
- Panics if there is a provider that panics on construction.
Sourcepub async fn create_async(self, modules: Vec<ResolveModule>) -> Context
pub async fn create_async(self, modules: Vec<ResolveModule>) -> Context
Async version of ContextOptions::create
.
If no provider in the context has an async constructor and that provider needs to be eagerly created,
this method is the same as ContextOptions::create
.
See ContextOptions::create
for more details.
§Panics
- Panics if there are multiple providers with the same key and the context’s
allow_override
is false. - Panics if there is a provider that panics on construction.
Sourcepub async fn auto_register_async(self) -> Context
Available on crate feature auto-register
only.
pub async fn auto_register_async(self) -> Context
auto-register
only.Async version of ContextOptions::auto_register
.
If no provider in the context has an async constructor and that provider needs to be eagerly created,
this method is the same as ContextOptions::auto_register
.
See ContextOptions::auto_register
for more details.
§Panics
- Panics if there are multiple providers with the same key and the context’s
allow_override
is false. - Panics if there is a provider that panics on construction.