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>

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, Global>> 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§

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> !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 Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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 Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.