Struct async_injector::Injector
source · 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
impl Injector
sourcepub fn new() -> Self
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());
sourcepub async fn clear<T>(&self) -> Option<T>
pub async fn clear<T>(&self) -> Option<T>
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);
sourcepub async fn clear_key<T>(&self, key: impl AsRef<Key<T>>) -> Option<T>
pub async fn clear_key<T>(&self, key: impl AsRef<Key<T>>) -> Option<T>
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);
sourcepub async fn update<T>(&self, value: T) -> Option<T>
pub async fn update<T>(&self, value: T) -> Option<T>
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);
sourcepub async fn update_key<T>(
&self,
key: impl AsRef<Key<T>>,
value: T
) -> Option<T>
pub async fn update_key<T>( &self, key: impl AsRef<Key<T>>, value: T ) -> Option<T>
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);
sourcepub async fn exists<T>(&self) -> bool
pub async fn exists<T>(&self) -> bool
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);
sourcepub async fn exists_key<T>(&self, key: impl AsRef<Key<T>>) -> bool
pub async fn exists_key<T>(&self, key: impl AsRef<Key<T>>) -> bool
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);
sourcepub async fn mutate<T, M, R>(&self, mutator: M) -> Option<R>
pub async fn mutate<T, M, R>(&self, mutator: M) -> Option<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);
sourcepub async fn mutate_key<T, M, R>(
&self,
key: impl AsRef<Key<T>>,
mutator: M
) -> Option<R>
pub async fn mutate_key<T, M, R>( &self, key: impl AsRef<Key<T>>, mutator: M ) -> Option<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);
sourcepub async fn get<T>(&self) -> Option<T>
pub async fn get<T>(&self) -> Option<T>
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);
sourcepub async fn get_key<T>(&self, key: impl AsRef<Key<T>>) -> Option<T>
pub async fn get_key<T>(&self, key: impl AsRef<Key<T>>) -> Option<T>
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);
sourcepub async fn wait<T>(&self) -> T
pub async fn wait<T>(&self) -> T
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);
sourcepub async fn wait_key<T>(&self, key: impl AsRef<Key<T>>) -> T
pub async fn wait_key<T>(&self, key: impl AsRef<Key<T>>) -> T
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);
sourcepub async fn stream<T>(&self) -> (Stream<T>, Option<T>)
pub async fn stream<T>(&self) -> (Stream<T>, Option<T>)
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);
sourcepub async fn stream_key<T>(
&self,
key: impl AsRef<Key<T>>
) -> (Stream<T>, Option<T>)
pub async fn stream_key<T>( &self, key: impl AsRef<Key<T>> ) -> (Stream<T>, Option<T>)
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);
sourcepub async fn var<T>(&self) -> Ref<T>
pub async fn var<T>(&self) -> Ref<T>
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());
sourcepub async fn var_key<T>(&self, key: impl AsRef<Key<T>>) -> Ref<T>
pub async fn var_key<T>(&self, key: impl AsRef<Key<T>>) -> Ref<T>
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());