Struct 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, set_finished};
// 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>

Source

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

Source

pub fn as_slice(&self) -> &[T]

get current data as slice, may be incomplete depending on status

Source

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>, String> for LazyVecPromise<T>

Source§

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>>

returns mutable reference to the cache if applicable
Source§

fn get_value(&self) -> Option<&Vec<T>>

returns a reference to the cache if applicable
Source§

fn get_result(&self) -> Option<Result<&Vec<T>, &String>>

returns a reference to the cache or to error if applicable
Source§

fn take_result(&mut self) -> Option<Result<Vec<T>, String>>

takes the value or error and leaves the promise in a valid state indicating its emptiness
Source§

impl<T: Debug> Promise for LazyVecPromise<T>

Source§

fn poll_state(&mut self) -> &DataState

Polls the promise, triggers update if state is DataState::Uninitialized
Source§

fn update(&mut self)

Clears the data cache and immediately triggers an update

Auto Trait Implementations§

§

impl<T> Freeze for LazyVecPromise<T>

§

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.