[−][src]Struct async_injector::Injector
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
impl Injector[src]
pub fn new() -> Self[src]
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(()) }
pub async fn clear<T>(&self) -> Option<T> where
T: Clone + Any + Send + Sync, [src]
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
#[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); }
pub async fn clear_key<T>(&self, key: impl AsRef<Key<T>>) -> Option<T> where
T: Clone + Any + Send + Sync, [src]
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}; 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(()) }
pub async fn update<T>(&self, value: T) -> Option<T> where
T: Clone + Any + Send + Sync, [src]
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
#[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); }
pub async fn update_key<T>(
&self,
key: impl AsRef<Key<T>>,
value: T
) -> Option<T> where
T: Clone + Any + Send + Sync, [src]
&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}; 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(()) }
pub async fn exists<T>(&self) -> bool where
T: Clone + Any + Send + Sync, [src]
T: Clone + Any + Send + Sync,
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); }
pub async fn exists_key<T>(&self, key: impl AsRef<Key<T>>) -> bool where
T: Clone + Any + Send + Sync, [src]
T: Clone + Any + Send + Sync,
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(()) }
pub async fn mutate<T, M, R>(&self, mutator: M) -> Option<R> where
T: Clone + Any + Send + Sync,
M: FnMut(&mut T) -> R, [src]
T: Clone + Any + Send + Sync,
M: FnMut(&mut T) -> R,
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); }
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, [src]
&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}; 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(()) }
pub async fn get<T>(&self) -> Option<T> where
T: Clone + Any + Send + Sync, [src]
T: Clone + Any + Send + Sync,
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); }
pub async fn get_key<T>(&self, key: impl AsRef<Key<T>>) -> Option<T> where
T: Clone + Any + Send + Sync, [src]
T: Clone + Any + Send + Sync,
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(()) }
pub async fn stream<T>(&self) -> (Stream<T>, Option<T>) where
T: Clone + Any + Send + Sync, [src]
T: Clone + Any + Send + Sync,
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); }
pub async fn stream_key<T>(
&self,
key: impl AsRef<Key<T>>
) -> (Stream<T>, Option<T>) where
T: Clone + Any + Send + Sync, [src]
&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::{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(()) }
pub async fn var<T>(&self) -> Ref<T> where
T: Clone + Any + Send + Sync + Unpin, [src]
T: Clone + Any + Send + Sync + Unpin,
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(()) }
pub async fn var_key<T>(&self, key: impl AsRef<Key<T>>) -> Ref<T> where
T: Clone + Any + Send + Sync + Unpin, [src]
T: Clone + Any + Send + Sync + Unpin,
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
Auto Trait Implementations
impl !RefUnwindSafe for Injector[src]
impl Send for Injector[src]
impl Sync for Injector[src]
impl Unpin for Injector[src]
impl !UnwindSafe for Injector[src]
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized, [src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized, [src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized, [src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T[src]
impl<T> From<T> for T[src]
impl<T, U> Into<U> for T where
U: From<T>, [src]
U: From<T>,
impl<T> ToOwned for T where
T: Clone, [src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
pub fn to_owned(&self) -> T[src]
pub fn clone_into(&self, target: &mut T)[src]
impl<T, U> TryFrom<U> for T where
U: Into<T>, [src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>, [src]
U: TryFrom<T>,