pub struct Injector { /* private fields */ }
Expand description

An injector of dependencies.

Injectors are defined in hierarchies where an injector is either the root injector as created using Injector::new.

Implementations§

source§

impl Injector

source

pub fn new() -> Self

Construct and use an Injector.

Example
use async_injector::Injector;

let injector = Injector::new();

assert_eq!(None, injector.get::<u32>().await);
injector.update(1u32).await;
assert_eq!(Some(1u32), injector.get::<u32>().await);
assert!(injector.clear::<u32>().await.is_some());
assert_eq!(None, injector.get::<u32>().await);

Example using a Stream.

use async_injector::Injector;

#[derive(Clone)]
struct Database;

let injector = Injector::new();

let database = injector.var::<Database>().await;

assert!(database.read().await.is_none());
injector.update(Database).await;
assert!(database.read().await.is_some());
source

pub async fn clear<T>(&self) -> Option<T>
where T: Clone + Any + Send + Sync,

Get a value from the injector.

This will cause the clear to be propagated to all streams set up using stream. And for future calls to get to return the updated value.

Examples
use async_injector::Injector;

let injector = Injector::new();

assert_eq!(None, injector.get::<u32>().await);
injector.update(1u32).await;
assert_eq!(Some(1u32), injector.get::<u32>().await);
assert!(injector.clear::<u32>().await.is_some());
assert_eq!(None, injector.get::<u32>().await);
source

pub async fn clear_key<T>(&self, key: impl AsRef<Key<T>>) -> Option<T>
where T: Clone + Any + Send + Sync,

Clear the given value with the given key.

This will cause the clear to be propagated to all streams set up using stream. And for future calls to get to return the updated value.

Examples
use async_injector::{Key, Injector};

let injector = Injector::new();
let k = Key::<u32>::tagged("first")?;

assert_eq!(None, injector.get_key(&k).await);
injector.update_key(&k, 1u32).await;
assert_eq!(Some(1u32), injector.get_key(&k).await);
assert!(injector.clear_key(&k).await.is_some());
assert_eq!(None, injector.get_key(&k).await);
source

pub async fn update<T>(&self, value: T) -> Option<T>
where T: Clone + Any + Send + Sync,

Set the given value and notify any subscribers.

This will cause the update to be propagated to all streams set up using stream. And for future calls to get to return the updated value.

Examples
use async_injector::Injector;

let injector = Injector::new();

assert_eq!(None, injector.get::<u32>().await);
injector.update(1u32).await;
assert_eq!(Some(1u32), injector.get::<u32>().await);
source

pub async fn update_key<T>( &self, key: impl AsRef<Key<T>>, value: T ) -> Option<T>
where T: Clone + Any + Send + Sync,

Update the value associated with the given key.

This will cause the update to be propagated to all streams set up using stream. And for future calls to get to return the updated value.

Examples
use async_injector::{Key, Injector};

let injector = Injector::new();
let k = Key::<u32>::tagged("first")?;

assert_eq!(None, injector.get_key(&k).await);
injector.update_key(&k, 1u32).await;
assert_eq!(Some(1u32), injector.get_key(&k).await);
source

pub async fn exists<T>(&self) -> bool
where T: Clone + Any + Send + Sync,

Test if a given value exists by type.

Examples
use async_injector::{Key, Injector};

let injector = Injector::new();

assert_eq!(false, injector.exists::<u32>().await);
injector.update(1u32).await;
assert_eq!(true, injector.exists::<u32>().await);
source

pub async fn exists_key<T>(&self, key: impl AsRef<Key<T>>) -> bool
where T: Clone + Any + Send + Sync,

Test if a given value exists by key.

Examples
use async_injector::{Key, Injector};

let injector = Injector::new();
let k = Key::<u32>::tagged("first")?;

assert_eq!(false, injector.exists_key(&k).await);
injector.update_key(&k, 1u32).await;
assert_eq!(true, injector.exists_key(&k).await);
source

pub async fn mutate<T, M, R>(&self, mutator: M) -> Option<R>
where T: Clone + Any + Send + Sync, M: FnMut(&mut T) -> R,

Mutate the given value by type.

Examples
use async_injector::Injector;

let injector = Injector::new();

injector.update(1u32).await;

let old = injector.mutate(|value: &mut u32| {
    let old = *value;
    *value += 1;
    old
}).await;

assert_eq!(Some(1u32), old);
source

pub async fn mutate_key<T, M, R>( &self, key: impl AsRef<Key<T>>, mutator: M ) -> Option<R>
where T: Clone + Any + Send + Sync, M: FnMut(&mut T) -> R,

Mutate the given value by key.

Examples
use async_injector::{Key, Injector};

let injector = Injector::new();
let k = Key::<u32>::tagged("first")?;

injector.update_key(&k, 1u32).await;

let old = injector.mutate_key(&k, |value| {
    let old = *value;
    *value += 1;
    old
}).await;

assert_eq!(Some(1u32), old);
source

pub async fn get<T>(&self) -> Option<T>
where T: Clone + Any + Send + Sync,

Get a value from the injector.

Examples
use async_injector::Injector;

let injector = Injector::new();

assert_eq!(None, injector.get::<u32>().await);
injector.update(1u32).await;
assert_eq!(Some(1u32), injector.get::<u32>().await);
source

pub async fn get_key<T>(&self, key: impl AsRef<Key<T>>) -> Option<T>
where T: Clone + Any + Send + Sync,

Get a value from the injector with the given key.

Examples
use async_injector::{Key, Injector};

let k1 = Key::<u32>::tagged("first")?;
let k2 = Key::<u32>::tagged("second")?;

let injector = Injector::new();

assert_eq!(None, injector.get_key(&k1).await);
assert_eq!(None, injector.get_key(&k2).await);

injector.update_key(&k1, 1u32).await;

assert_eq!(Some(1u32), injector.get_key(&k1).await);
assert_eq!(None, injector.get_key(&k2).await);
source

pub async fn wait<T>(&self) -> T
where T: Clone + Any + Send + Sync,

Wait for a value to become available.

Note that this could potentially wait forever if the value is never injected.

Examples
use async_injector::Injector;

let injector = Injector::new();

injector.update(1u32).await;
assert_eq!(1u32, injector.wait::<u32>().await);
source

pub async fn wait_key<T>(&self, key: impl AsRef<Key<T>>) -> T
where T: Clone + Any + Send + Sync,

Wait for a value associated with the given key to become available.

Note that this could potentially wait forever if the value is never injected.

Examples
use async_injector::{Key, Injector};

let injector = Injector::new();
let tag = Key::<u32>::tagged("first")?;

injector.update_key(&tag, 1u32).await;
assert_eq!(1u32, injector.wait_key(tag).await);
source

pub async fn stream<T>(&self) -> (Stream<T>, Option<T>)
where T: Clone + Any + Send + Sync,

Get an existing value and setup a stream for updates at the same time.

Examples
use async_injector::Injector;

#[derive(Debug, Clone, PartialEq, Eq)]
struct Database;

let injector = Injector::new();
let (mut database_stream, mut database) = injector.stream::<Database>().await;

// Update the key somewhere else.
tokio::spawn({
    let injector = injector.clone();

    async move {
        injector.update(Database).await;
    }
});

let database = loop {
    if let Some(update) = database_stream.recv().await {
        break update;
    }
};

assert_eq!(database, Database);
source

pub async fn stream_key<T>( &self, key: impl AsRef<Key<T>> ) -> (Stream<T>, Option<T>)
where T: Clone + Any + Send + Sync,

Get an existing value and setup a stream for updates at the same time.

Examples
use async_injector::{Key, Injector};

#[derive(Debug, Clone, PartialEq, Eq)]
struct Database;

let injector = Injector::new();
let db = Key::<Database>::tagged("first")?;
let (mut database_stream, mut database) = injector.stream_key(&db).await;

// Update the key somewhere else.
tokio::spawn({
    let db = db.clone();
    let injector = injector.clone();

    async move {
        injector.update_key(&db, Database).await;
    }
});

let database = loop {
    if let Some(update) = database_stream.recv().await {
        break update;
    }
};

assert_eq!(database, Database);
source

pub async fn var<T>(&self) -> Ref<T>
where T: Clone + Any + Send + Sync + Unpin,

Get a synchronized reference for the given configuration key.

Examples
use async_injector::Injector;

#[derive(Clone)]
struct Database;

let injector = Injector::new();

let database = injector.var::<Database>().await;

assert!(database.read().await.is_none());
injector.update(Database).await;
assert!(database.read().await.is_some());
source

pub async fn var_key<T>(&self, key: impl AsRef<Key<T>>) -> Ref<T>
where T: Clone + Any + Send + Sync + Unpin,

Get a synchronized reference for the given configuration key.

Examples
use async_injector::{Key, Injector};
use std::error::Error;

#[derive(Clone)]
struct Database;

let injector = Injector::new();
let db = Key::<Database>::tagged("first")?;

let database = injector.var_key(&db).await;

assert!(database.read().await.is_none());
injector.update_key(&db, Database).await;
assert!(database.read().await.is_some());

Trait Implementations§

source§

impl Clone for Injector

source§

fn clone(&self) -> Injector

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Default for Injector

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, 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> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.