pub struct Deferred<T> { /* private fields */ }Expand description
A background task whose result you can check or collect later.
Tasks run on any async runtime through a Spawner. With the default tokio
feature, start and begin spawn on the current
Tokio runtime.
The result is kept until you take it or drop the Deferred.
Dropping a Deferred does not stop its task; use cancel for that.
§Examples
use async_deferred::Deferred;
let mut deferred = Deferred::start(async { 42 });
// ... do other work ...
assert_eq!(deferred.join().await, Ok(&42));Implementations§
Source§impl<T> Deferred<T>
impl<T> Deferred<T>
Sourcepub fn start_on<S, F>(spawner: &S, future: F) -> Self
pub fn start_on<S, F>(spawner: &S, future: F) -> Self
Spawns future with spawner and returns its Deferred.
See Spawner for using your own runtime.
use async_deferred::Deferred;
// Start a task from outside a Tokio runtime
let runtime = tokio::runtime::Runtime::new().unwrap();
let mut deferred = Deferred::start_on(runtime.handle(), async { 42 });
assert_eq!(runtime.block_on(deferred.join()), Ok(&42));Sourcepub fn start_with_callback_on<S, F, C>(
spawner: &S,
future: F,
callback: C,
) -> Self
pub fn start_with_callback_on<S, F, C>( spawner: &S, future: F, callback: C, ) -> Self
Like start_on, and runs callback with the result once
future finishes.
The callback is not run if future panics.
Sourcepub fn begin_on<S, F>(&mut self, spawner: &S, future: F) -> bool
pub fn begin_on<S, F>(&mut self, spawner: &S, future: F) -> bool
Spawns future with spawner.
Returns false and does nothing if a task was already started and its result
has not been taken.
Sourcepub fn begin_with_callback_on<S, F, C>(
&mut self,
spawner: &S,
future: F,
callback: C,
) -> bool
pub fn begin_with_callback_on<S, F, C>( &mut self, spawner: &S, future: F, callback: C, ) -> bool
Like begin_on, and runs callback with the result once
future finishes.
The callback is not run if future panics.
Sourcepub fn start_local_on<S, F>(spawner: &S, future: F) -> Selfwhere
S: LocalSpawner,
F: Future<Output = T> + 'static,
T: 'static,
pub fn start_local_on<S, F>(spawner: &S, future: F) -> Selfwhere
S: LocalSpawner,
F: Future<Output = T> + 'static,
T: 'static,
Like start_on, for futures that are not Send.
Sourcepub fn start_with_callback_local_on<S, F, C>(
spawner: &S,
future: F,
callback: C,
) -> Self
pub fn start_with_callback_local_on<S, F, C>( spawner: &S, future: F, callback: C, ) -> Self
Like start_with_callback_on, for futures that
are not Send.
Sourcepub fn begin_local_on<S, F>(&mut self, spawner: &S, future: F) -> boolwhere
S: LocalSpawner,
F: Future<Output = T> + 'static,
T: 'static,
pub fn begin_local_on<S, F>(&mut self, spawner: &S, future: F) -> boolwhere
S: LocalSpawner,
F: Future<Output = T> + 'static,
T: 'static,
Like begin_on, for futures that are not Send.
Sourcepub fn begin_with_callback_local_on<S, F, C>(
&mut self,
spawner: &S,
future: F,
callback: C,
) -> bool
pub fn begin_with_callback_local_on<S, F, C>( &mut self, spawner: &S, future: F, callback: C, ) -> bool
Like begin_with_callback_on, for futures that
are not Send.
Sourcepub async fn join(&mut self) -> Result<&T, Error>
pub async fn join(&mut self) -> Result<&T, Error>
Waits for the task to finish and returns its result.
Returns immediately if the task already finished. If the task finished but its
callback panicked, the result is still returned; check state
to detect that.
use async_deferred::{Deferred, Error};
let mut deferred: Deferred<u32> = Deferred::start(async { panic!("boom") });
assert_eq!(deferred.join().await, Err(Error::Panicked("boom".into())));Sourcepub fn panic_message(&mut self) -> Option<&str>
pub fn panic_message(&mut self) -> Option<&str>
Returns the panic message if the task or its callback panicked.
Sourcepub fn try_get(&mut self) -> Option<&T>
pub fn try_get(&mut self) -> Option<&T>
Returns the result if the task has finished, without waiting.
Sourcepub fn take(&mut self) -> Option<T>
pub fn take(&mut self) -> Option<T>
Moves the result out if the task has finished, without waiting.
On success, the Deferred is reset and can start a new task.
use async_deferred::{Deferred, State};
let mut deferred = Deferred::start(async { vec![1, 2, 3] });
deferred.join().await.unwrap();
assert_eq!(deferred.take(), Some(vec![1, 2, 3]));
assert_eq!(deferred.state(), State::NotStarted);Sourcepub fn cancel(&mut self) -> bool
pub fn cancel(&mut self) -> bool
Stops a running task and resets the Deferred so it can start a new one.
The task stops the next time the runtime polls it.
Returns false and does nothing if no task is running.
use async_deferred::{Deferred, State};
let mut deferred = Deferred::start(std::future::pending::<u32>());
assert!(deferred.cancel());
assert_eq!(deferred.state(), State::NotStarted);Sourcepub fn is_ready(&mut self) -> bool
pub fn is_ready(&mut self) -> bool
Returns true if the result is available, even if the callback panicked.
Sourcepub fn is_complete(&mut self) -> bool
pub fn is_complete(&mut self) -> bool
Returns true if the task and its callback finished without panicking.
Sourcepub fn is_pending(&mut self) -> bool
pub fn is_pending(&mut self) -> bool
Returns true if the task is still running.
Sourcepub fn is_not_started(&mut self) -> bool
pub fn is_not_started(&mut self) -> bool
Returns true if no task has been started, or its result was taken.
Sourcepub fn has_task_panicked(&mut self) -> bool
pub fn has_task_panicked(&mut self) -> bool
Returns true if the task panicked.
Source§impl<T> Deferred<T>
impl<T> Deferred<T>
Sourcepub fn start<F>(future: F) -> Self
pub fn start<F>(future: F) -> Self
Spawns future on the current Tokio runtime and returns its Deferred.
§Panics
Panics if called outside a Tokio runtime. Use start_on with a
tokio::runtime::Handle there.
Sourcepub fn start_with_callback<F, C>(future: F, callback: C) -> Self
pub fn start_with_callback<F, C>(future: F, callback: C) -> Self
Like start, and runs callback with the result once future finishes.
The callback is not run if future panics.
use async_deferred::Deferred;
let deferred = Deferred::start_with_callback(
async { 42 },
|result| println!("computed {result}"),
);Sourcepub fn begin<F>(&mut self, future: F) -> bool
pub fn begin<F>(&mut self, future: F) -> bool
Spawns future on the current Tokio runtime.
Returns false and does nothing if a task was already started and its result
has not been taken.
§Panics
Panics if called outside a Tokio runtime.
use async_deferred::Deferred;
let mut deferred = Deferred::new();
assert!(deferred.begin(async { 42 }));
assert!(!deferred.begin(async { 24 }));