Skip to main content

Deferred

Struct Deferred 

Source
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>

Source

pub fn new() -> Self

Creates a Deferred with no task. Start one with begin_on.

Source

pub fn start_on<S, F>(spawner: &S, future: F) -> Self
where S: Spawner, F: Future<Output = T> + Send + 'static, T: Send + 'static,

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

pub fn start_with_callback_on<S, F, C>( spawner: &S, future: F, callback: C, ) -> Self
where S: Spawner, F: Future<Output = T> + Send + 'static, C: FnOnce(&T) + Send + 'static, T: Send + 'static,

Like start_on, and runs callback with the result once future finishes.

The callback is not run if future panics.

Source

pub fn begin_on<S, F>(&mut self, spawner: &S, future: F) -> bool
where S: Spawner, F: Future<Output = T> + Send + 'static, T: Send + 'static,

Spawns future with spawner.

Returns false and does nothing if a task was already started and its result has not been taken.

Source

pub fn begin_with_callback_on<S, F, C>( &mut self, spawner: &S, future: F, callback: C, ) -> bool
where S: Spawner, F: Future<Output = T> + Send + 'static, C: FnOnce(&T) + Send + 'static, T: Send + 'static,

Like begin_on, and runs callback with the result once future finishes.

The callback is not run if future panics.

Source

pub fn start_local_on<S, F>(spawner: &S, future: F) -> Self
where S: LocalSpawner, F: Future<Output = T> + 'static, T: 'static,

Like start_on, for futures that are not Send.

Source

pub fn start_with_callback_local_on<S, F, C>( spawner: &S, future: F, callback: C, ) -> Self
where S: LocalSpawner, F: Future<Output = T> + 'static, C: FnOnce(&T) + 'static, T: 'static,

Like start_with_callback_on, for futures that are not Send.

Source

pub fn begin_local_on<S, F>(&mut self, spawner: &S, future: F) -> bool
where S: LocalSpawner, F: Future<Output = T> + 'static, T: 'static,

Like begin_on, for futures that are not Send.

Source

pub fn begin_with_callback_local_on<S, F, C>( &mut self, spawner: &S, future: F, callback: C, ) -> bool
where S: LocalSpawner, F: Future<Output = T> + 'static, C: FnOnce(&T) + 'static, T: 'static,

Like begin_with_callback_on, for futures that are not Send.

Source

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

pub fn state(&mut self) -> State

Returns the current State without waiting.

Source

pub fn panic_message(&mut self) -> Option<&str>

Returns the panic message if the task or its callback panicked.

Source

pub fn try_get(&mut self) -> Option<&T>

Returns the result if the task has finished, without waiting.

Source

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

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

pub fn is_ready(&mut self) -> bool

Returns true if the result is available, even if the callback panicked.

Source

pub fn is_complete(&mut self) -> bool

Returns true if the task and its callback finished without panicking.

Source

pub fn is_pending(&mut self) -> bool

Returns true if the task is still running.

Source

pub fn is_not_started(&mut self) -> bool

Returns true if no task has been started, or its result was taken.

Source

pub fn has_task_panicked(&mut self) -> bool

Returns true if the task panicked.

Source§

impl<T> Deferred<T>

Source

pub fn start<F>(future: F) -> Self
where F: Future<Output = T> + Send + 'static, T: Send + 'static,

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.

Source

pub fn start_with_callback<F, C>(future: F, callback: C) -> Self
where F: Future<Output = T> + Send + 'static, C: FnOnce(&T) + Send + 'static, T: Send + 'static,

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}"),
);
Source

pub fn begin<F>(&mut self, future: F) -> bool
where F: Future<Output = T> + Send + 'static, T: Send + 'static,

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

pub fn begin_with_callback<F, C>(&mut self, future: F, callback: C) -> bool
where F: Future<Output = T> + Send + 'static, C: FnOnce(&T) + Send + 'static, T: Send + 'static,

Like begin, and runs callback with the result once future finishes.

The callback is not run if future panics.

Trait Implementations§

Source§

impl<T: Debug> Debug for Deferred<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T> Default for Deferred<T>

Source§

fn default() -> Self

Same as Deferred::new.

Auto Trait Implementations§

§

impl<T> !RefUnwindSafe for Deferred<T>

§

impl<T> !UnwindSafe for Deferred<T>

§

impl<T> Freeze for Deferred<T>
where Inner<T>: Freeze,

§

impl<T> Send for Deferred<T>
where Inner<T>: Send,

§

impl<T> Sync for Deferred<T>
where Inner<T>: Sync,

§

impl<T> Unpin for Deferred<T>
where Inner<T>: Unpin,

§

impl<T> UnsafeUnpin for Deferred<T>
where Inner<T>: UnsafeUnpin,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.