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

creates a new LazyVecPromise given an updater functor and a tokio buffer size

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

Trait Implementations

Polls the promise, triggers update if state is DataState::Uninitialized
Clears the data cache and immediately triggers an update

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.