Struct LazyValuePromise

Source
pub struct LazyValuePromise<T: Debug> { /* private fields */ }
Expand description

§A single lazy-async updated value

Create one with the LazyValuePromise::new method and supply an updater. It’s Updated only on first try to poll it making it scale nicely on more complex UIs. While still updating, the value can be read out already, this can make sense for iterative algorithms where an intermediate result can be used as a preview. Examples:

use std::time::Duration;
use tokio::sync::mpsc::Sender;
use lazy_async_promise::{DirectCacheAccess, DataState, Message, Promise, LazyValuePromise, api_macros::*};
// updater-future:
let updater = |tx: Sender<Message<i32>>| async move {
  send_data!(1337, 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);
  tokio::time::sleep(Duration::from_millis(100)).await;
  set_finished!(tx);
};
// direct usage:
let promise = LazyValuePromise::new(updater, 10);
// for usage of the progress, see the docs of [`LazyVecPromise`]
fn main_loop(mut  lazy_promise: LazyValuePromise<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!("Value up2date: {}", lazy_promise.get_value().unwrap()); }
                          _ => { println!("Still updating... might be in strange state! (current state: {:?}", lazy_promise.get_value()); }
    }
  }

  // Also, we can use all of DirectCacheAccess:
   let current_cache = lazy_promise.get_value();
   let current_cache_mut = lazy_promise.get_value_mut();
}

Implementations§

Source§

impl<T: Debug> LazyValuePromise<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 LazyValuePromise given an Updater and a tokio buffer size

Trait Implementations§

Source§

impl<T: Debug> DirectCacheAccess<T, String> for LazyValuePromise<T>

Source§

fn get_value_mut(&mut self) -> Option<&mut T>

get current value (may be incomplete) as mutable ref, be careful with this as further modification from the future may still push data.

Source§

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

get current value, may be incomplete depending on status

Source§

fn take_value(&mut self) -> Option<T>

takes the current value, if data was DataState::UpToDate it returns the value and sets the state to DataState::Uninitialized. Otherwise, returns None.

Source§

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

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

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

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

impl<T: Debug> Promise for LazyValuePromise<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 LazyValuePromise<T>
where T: Freeze,

§

impl<T> !RefUnwindSafe for LazyValuePromise<T>

§

impl<T> !Send for LazyValuePromise<T>

§

impl<T> !Sync for LazyValuePromise<T>

§

impl<T> Unpin for LazyValuePromise<T>
where T: Unpin,

§

impl<T> !UnwindSafe for LazyValuePromise<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.