Struct rudi::ContextOptions

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

source

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());
source

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());
source

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());
source

pub fn singleton<T>(self, instance: T) -> Self
where T: 'static + Clone,

Appends a standalone Singleton instance to the context with default name "".

§Example
use rudi::{modules, Context, ContextOptions};

let cx: Context = ContextOptions::default().singleton(42).create(modules![]);
assert_eq!(cx.get_single::<i32>(), &42);
source

pub fn singleton_with_name<T, N>(self, instance: T, name: N) -> Self
where T: 'static + Clone, N: Into<Cow<'static, str>>,

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);
source

pub fn single_owner<T>(self, instance: T) -> Self
where 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));
source

pub fn single_owner_with_name<T, N>(self, instance: T, name: N) -> Self
where T: 'static, N: Into<Cow<'static, str>>,

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));
source

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());
source

pub fn auto_register(self) -> Context

Available on crate feature 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.
source

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

pub async fn auto_register_async(self) -> Context

Available on crate feature 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.

Trait Implementations§

source§

impl Default for ContextOptions

source§

fn default() -> Self

Returns the “default value” for a type. 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> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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>,

§

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

§

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> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more