pub struct ImmediateValuePromise<T: Send + 'static> { /* private fields */ }
Expand description

A promise which can be easily created and stored.

Will spawn a task to resolve the future immediately. No possibility to read out intermediate values or communicate progress. One can use Option<ImmediateValuePromise<T>> inside state structs to make this class somewhat lazy. That may be an option if you don’t need any progress indication or intermediate values.

use std::fs::File;
use std::thread;
use std::time::Duration;
use lazy_async_promise::{ImmediateValuePromise, ImmediateValueState};
let mut oneshot_val = ImmediateValuePromise::new(async {
    tokio::time::sleep(Duration::from_millis(50)).await;
    let test_error_handling = false;
    if test_error_handling {
      // We can use the ?-operator for most errors in our futures
      let _file = File::open("I_DONT_EXIST_ERROR")?;
    }
    // return the value wrapped in Ok for the result here
    Ok(34)
});
assert!(matches!(
    oneshot_val.poll_state(),
    ImmediateValueState::Updating
));
thread::sleep(Duration::from_millis(100));
let result = oneshot_val.poll_state();
if let ImmediateValueState::Success(val) = result {
    assert_eq!(*val, 34);
} else {
    unreachable!();
}

Implementations

Creator, supply a future which returns Result<T, Box<dyn Error + Send>. Will be immediately spawned via tokio.

Poll the state, will return the data or error if ready or updating otherwise.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.