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
sourceimpl<T: Send> ImmediateValuePromise<T>
impl<T: Send> ImmediateValuePromise<T>
sourcepub fn new<U: Future<Output = Result<T, BoxedSendError>> + Send + 'static>(
updater: U
) -> Self
pub fn new<U: Future<Output = Result<T, BoxedSendError>> + Send + 'static>(
updater: U
) -> Self
Creator, supply a future which returns Result<T, Box<dyn Error + Send>
. Will be immediately spawned via tokio.
sourcepub fn poll_state(&mut self) -> &ImmediateValueState<T>
pub fn poll_state(&mut self) -> &ImmediateValueState<T>
Poll the state, will return the data or error if ready or updating otherwise.
Auto Trait Implementations
impl<T> !RefUnwindSafe for ImmediateValuePromise<T>
impl<T> Send for ImmediateValuePromise<T>
impl<T> !Sync for ImmediateValuePromise<T>
impl<T> Unpin for ImmediateValuePromise<T>where
T: Unpin,
impl<T> !UnwindSafe for ImmediateValuePromise<T>
Blanket Implementations
sourceimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more