Struct async_injector::Ref
source · [−]Expand description
A variable allowing for the synchronized reading of avalue in the Injector.
This can be created through Injector::var or Injector::var_key.
Implementations
sourceimpl<T> Ref<T> where
T: Clone + Any + Send + Sync,
impl<T> Ref<T> where
T: Clone + Any + Send + Sync,
sourcepub async fn read(&self) -> Option<RefReadGuard<'_, T>>
pub async fn read(&self) -> Option<RefReadGuard<'_, T>>
Read the synchronized variable.
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(())
}
sourcepub async fn load(&self) -> Option<T>
pub async fn load(&self) -> Option<T>
Load the synchronized variable. This clones the underlying value if it has been set.
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.load().await.is_none());
injector.update(Database).await;
assert!(database.load().await.is_some());
Ok(())
}
Trait Implementations
Auto Trait Implementations
impl<T> !RefUnwindSafe for Ref<T>
impl<T> Send for Ref<T>
impl<T> Sync for Ref<T>
impl<T> Unpin for Ref<T> where
T: Unpin,
impl<T> !UnwindSafe for Ref<T>
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcepub fn borrow_mut(&mut self) -> &mut T
pub fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcepub fn to_owned(&self) -> T
pub fn to_owned(&self) -> T
Creates owned data from borrowed data, usually by cloning. Read more
sourcepub fn clone_into(&self, target: &mut T)
pub fn clone_into(&self, target: &mut T)
🔬 This is a nightly-only experimental API. (
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more