Struct lazy_async_promise::LazyVecPromise
source · [−]pub struct LazyVecPromise<T: Debug> { /* private fields */ }Expand description
A lazy, async and partially readable vector promise
This promise is the right one for async acquiring of lists which should be partially readable on each frame. Imagine slowly streaming data and wanting to read them out as far as they are available each frame. Examples:
use std::time::Duration;
use tokio::sync::mpsc::Sender;
use lazy_async_promise::{DataState, Message, Promise};
use lazy_async_promise::LazyVecPromise;
use lazy_async_promise::unpack_result;
// updater-future:
let updater = |tx: Sender<Message<i32>>| async move {
for i in 0..100 {
tx.send(Message::NewData(i)).await.unwrap();
// 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);
if i > 100 {
tx.send(Message::StateChange(DataState::Error("loop overflow".to_owned()))).await.unwrap();
}
tokio::time::sleep(Duration::from_millis(100)).await;
}
tx.send(Message::StateChange(DataState::UpToDate)).await.unwrap();
};
// direct usage:
let promise = LazyVecPromise::new(updater, 10);
fn main_loop(lazy_promise: &mut LazyVecPromise<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!("Data complete: {:?}", lazy_promise.as_slice()); },
_ => { println!("Getting data, might be partially ready! (part: {:?}", lazy_promise.as_slice()); }
}
}
}Implementations
Trait Implementations
sourceimpl<T: Debug> Promise for LazyVecPromise<T>
impl<T: Debug> Promise for LazyVecPromise<T>
sourcefn 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> !RefUnwindSafe for LazyVecPromise<T>
impl<T> !Send for LazyVecPromise<T>
impl<T> !Sync for LazyVecPromise<T>
impl<T> Unpin for LazyVecPromise<T>where
T: Unpin,
impl<T> !UnwindSafe for LazyVecPromise<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