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, LazyVecPromise, api_macros::*, DirectCacheAccess};
// updater-future:
let updater = |tx: Sender<Message<i32>>| async move {
const ITEM_COUNT: i32 = 100;
for i in 0..ITEM_COUNT {
send_data!(i, tx);
set_progress!(Progress::from_fraction(i, ITEM_COUNT), 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);
if i > 100 {
set_error!("loop overflow".to_owned(), tx);
}
tokio::time::sleep(Duration::from_millis(100)).await;
}
set_finished!(tx);
};
// direct usage:
let promise = LazyVecPromise::new(updater, 200);
fn main_loop(mut lazy_promise: LazyVecPromise<i32>) {
loop {
let state = lazy_promise.poll_state();
let progress = state.get_progress().unwrap_or_default();
match state {
DataState::Error(er) => { println!("Error {} occurred! Retrying!", er); std::thread::sleep(Duration::from_millis(50)); lazy_promise.update(); }
DataState::UpToDate => { println!("Data complete: {:?}", lazy_promise.as_slice()); }
_ => { println!("Getting data, might be partially ready! (part: {:?} - progress: {}", lazy_promise.as_slice(), progress.as_f32()); }
}
}
// Also, we can use DirectCacheAccess:
let current_cache = lazy_promise.get_value();
let current_cache_mut = lazy_promise.get_value_mut();
}
Implementations§
source§impl<T: Debug> LazyVecPromise<T>
impl<T: Debug> LazyVecPromise<T>
sourcepub fn new<U: Fn(Sender<Message<T>>) -> Fut + 'static, Fut: Future<Output = ()> + Send + 'static>(
future_factory: U,
buffer_size: usize
) -> Self
pub fn new<U: Fn(Sender<Message<T>>) -> Fut + 'static, Fut: Future<Output = ()> + Send + 'static>( future_factory: U, buffer_size: usize ) -> Self
creates a new LazyVecPromise given an updater functor and a tokio buffer size
sourcepub fn as_slice(&self) -> &[T]
pub fn as_slice(&self) -> &[T]
get current data as slice, may be incomplete depending on status
sourcepub fn as_slice_mut(&mut self) -> &mut [T]
pub fn as_slice_mut(&mut self) -> &mut [T]
Get the current data as mutable slice
Trait Implementations§
source§impl<T: Debug> DirectCacheAccess<Vec<T, Global>> for LazyVecPromise<T>
impl<T: Debug> DirectCacheAccess<Vec<T, Global>> for LazyVecPromise<T>
source§fn take_value(&mut self) -> Option<Vec<T>>
fn take_value(&mut self) -> Option<Vec<T>>
Take the current data. If state was DataState::UpToDate
it will return the value.
If the state was anything else, it will return None. If data is taken successfully, will leave
the object in state DataState::Uninitialized
source§fn get_value_mut(&mut self) -> Option<&mut Vec<T>>
fn get_value_mut(&mut self) -> Option<&mut Vec<T>>
returns mutable reference to the cache if applicable
source§impl<T: Debug> Promise for LazyVecPromise<T>
impl<T: Debug> Promise for LazyVecPromise<T>
source§fn poll_state(&mut self) -> &DataState
fn poll_state(&mut self) -> &DataState
Polls the promise, triggers update if state is
DataState::Uninitialized
Auto 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§
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