pub struct LazyValuePromise<T: Debug> { /* private fields */ }Expand description
§A single lazy-async updated value
Create one with the LazyValuePromise::new method and supply an updater.
It’s Updated only on first try to poll it making it scale nicely on more complex UIs.
While still updating, the value can be read out already, this can make sense for iterative algorithms where an intermediate result can be used as a preview.
Examples:
use std::time::Duration;
use tokio::sync::mpsc::Sender;
use lazy_async_promise::{DirectCacheAccess, DataState, Message, Promise, LazyValuePromise, api_macros::*};
// updater-future:
let updater = |tx: Sender<Message<i32>>| async move {
send_data!(1337, tx);
// how to handle results and propagate the error to the future? Use `unpack_result!`:
let string = unpack_result!(std::fs::read_to_string("whatever.txt"), tx);
tokio::time::sleep(Duration::from_millis(100)).await;
set_finished!(tx);
};
// direct usage:
let promise = LazyValuePromise::new(updater, 10);
// for usage of the progress, see the docs of [`LazyVecPromise`]
fn main_loop(mut lazy_promise: LazyValuePromise<i32>) {
loop {
match lazy_promise.poll_state() {
DataState::Error(er) => { println!("Error {} occurred! Retrying!", er); std::thread::sleep(Duration::from_millis(500)); lazy_promise.update(); }
DataState::UpToDate => { println!("Value up2date: {}", lazy_promise.get_value().unwrap()); }
_ => { println!("Still updating... might be in strange state! (current state: {:?}", lazy_promise.get_value()); }
}
}
// Also, we can use all of DirectCacheAccess:
let current_cache = lazy_promise.get_value();
let current_cache_mut = lazy_promise.get_value_mut();
}Implementations§
Trait Implementations§
Source§impl<T: Debug> DirectCacheAccess<T, String> for LazyValuePromise<T>
impl<T: Debug> DirectCacheAccess<T, String> for LazyValuePromise<T>
Source§fn get_value_mut(&mut self) -> Option<&mut T>
fn get_value_mut(&mut self) -> Option<&mut T>
get current value (may be incomplete) as mutable ref, be careful with this as further modification from the future may still push data.
Source§fn take_value(&mut self) -> Option<T>
fn take_value(&mut self) -> Option<T>
takes the current value, if data was DataState::UpToDate it returns the value and sets the state to
DataState::Uninitialized. Otherwise, returns None.
Source§impl<T: Debug> Promise for LazyValuePromise<T>
impl<T: Debug> Promise for LazyValuePromise<T>
Source§fn poll_state(&mut self) -> &DataState
fn poll_state(&mut self) -> &DataState
Polls the promise, triggers update if state is
DataState::UninitializedAuto Trait Implementations§
impl<T> Freeze for LazyValuePromise<T>where
T: Freeze,
impl<T> !RefUnwindSafe for LazyValuePromise<T>
impl<T> !Send for LazyValuePromise<T>
impl<T> !Sync for LazyValuePromise<T>
impl<T> Unpin for LazyValuePromise<T>where
T: Unpin,
impl<T> !UnwindSafe for LazyValuePromise<T>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more