Struct async_injector::Injector[][src]

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

An injector of dependencies.

Injectors are defined in hierarchies where an injector is either the root injector as created using [setup] or [setup_with_driver], or the child of another injector through [Injector::child].

Child injectors receive all the updates of their ancestors.

Implementations

Construct an Injector.

Example

#[tokio::main]
async fn main() {
    let injector = async_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 std::error::Error;

#[derive(Clone)]
struct Database;

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
    let injector = async_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());
    Ok(())
}

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

#[tokio::main]
async fn main() {
    let injector = async_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);
}

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};
use std::error::Error;

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
    let injector = async_injector::Injector::new();
    let k = Key::<u32>::tagged("foo")?;

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

    Ok(())
}

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

#[tokio::main]
async fn main() {
    let injector = async_injector::Injector::new();

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

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};
use std::error::Error;

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
    let injector = async_injector::Injector::new();
    let k = Key::<u32>::tagged("foo")?;

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

    Ok(())
}

Test if a given value exists by type.

Examples

#[tokio::main]
async fn main() {
    let injector = async_injector::Injector::new();

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

Test if a given value exists by key.

Examples

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

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
    let injector = async_injector::Injector::new();
    let k = Key::<u32>::tagged("foo")?;

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

    Ok(())
}

Mutate the given value by type.

Examples

#[tokio::main]
async fn main() {
    let injector = async_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);
}

Mutate the given value by key.

Examples

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

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
    let injector = async_injector::Injector::new();
    let k = Key::<u32>::tagged("foo")?;

    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);
    Ok(())
}

Get a value from the injector.

Examples

#[tokio::main]
async fn main() {
    let injector = async_injector::Injector::new();

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

Get a value from the injector with the given key.

Examples

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

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
    let k1 = Key::<u32>::tagged("foo")?;
    let k2 = Key::<u32>::tagged("bar")?;

    let injector = async_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);

    Ok(())
}

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

Examples

use std::error::Error;

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

#[tokio::main]
async fn main() {
    let injector = async_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);
}

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

Examples

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

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

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
    let injector = async_injector::Injector::new();
    let db = Key::<Database>::tagged("foo")?;
    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);
    Ok(())
}

Get a synchronized reference for the given configuration key.

Examples

use std::error::Error;

#[derive(Clone)]
struct Database;

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
    let injector = async_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());
    Ok(())
}

Get a synchronized reference for the given configuration key.

Examples

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

#[derive(Clone)]
struct Database;

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
    let injector = async_injector::Injector::new();
    let db = Key::<Database>::tagged("foo")?;

    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());
    Ok(())
}

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The resulting type after obtaining ownership.

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

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.