Skip to content

Commit

Permalink
Merge pull request #5 from ChrisRega/0.3.0-dev
Browse files Browse the repository at this point in the history
Refine Progress-API, bump version to 0.3.0
  • Loading branch information
ChrisRega committed Nov 1, 2022
2 parents a4751a2 + 9c4fae9 commit 73ab695
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 21 deletions.
1 change: 0 additions & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ jobs:
include:
- os: ubuntu-latest
- os: windows-latest
- os: macos-latest

name: rust-ci ${{ matrix.os }}
runs-on: ${{ matrix.os }}
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "lazy_async_promise"
version = "0.3.0-RC1"
version = "0.3.0"
edition = "2021"
authors = ["Christopher Regali <[email protected]>"]
license = "MIT"
Expand Down
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ Example-usage of this crate with a small egui/eframe blog-reader can be found [h

Changelog:

0.3.0-RC1:
0.3.0:
- Removed `into_boxed` trait in favor of supporting the regular `From` trait which allows direct usage of the ?-Operator in `ImmediateValuePromise`
- Added a progress indicator for the `LazyVecPromise` and `LazyValuePromise`
- Added a progress indicator for the `LazyVecPromise` and `LazyValuePromise`
- Indication is done by `Progress` strong type which ensures values between 0.0 and 1.0
- Added CI for mac, windows and linux - had to give test-timings more leeway for mac os than on linux.
12 changes: 9 additions & 3 deletions src/lazyvalue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use tokio::sync::mpsc::{channel, Receiver, Sender};
/// };
/// // direct usage:
/// let promise = LazyValuePromise::new(updater, 10);
///
/// // for usage of the progress, see the docs of [`LazyVecPromise`]
/// fn main_loop(lazy_promise: &mut LazyValuePromise<i32>) {
/// loop {
/// match lazy_promise.poll_state() {
Expand Down Expand Up @@ -139,15 +139,21 @@ mod test {
//be lazy:
assert!(delayed_value.is_uninitialized());
//start sets updating
assert_eq!(*delayed_value.poll_state(), DataState::Updating(0.0.into()));
assert_eq!(
delayed_value.poll_state().get_progress().unwrap().as_f64(),
0.0
);
assert!(delayed_value.value().is_none());
//after wait, value is there
tokio::time::sleep(Duration::from_millis(150)).await;
assert_eq!(*delayed_value.poll_state(), DataState::UpToDate);
assert_eq!(delayed_value.value().unwrap(), "1");
//update resets
delayed_value.update();
assert_eq!(*delayed_value.poll_state(), DataState::Updating(0.0.into()));
assert_eq!(
delayed_value.poll_state().get_progress().unwrap().as_f64(),
0.0
);
assert!(delayed_value.value().is_none());
//after wait, value is there again and identical
tokio::time::sleep(Duration::from_millis(150)).await;
Expand Down
25 changes: 12 additions & 13 deletions src/lazyvec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,12 @@ use tokio::sync::mpsc::{channel, Receiver, Sender};
///
/// fn main_loop(lazy_promise: &mut LazyVecPromise<i32>) {
/// loop {
/// match lazy_promise.poll_state() {
/// 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: {:?}", lazy_promise.as_slice()); }
/// _ => { println!("Getting data, might be partially ready! (part: {:?} - progress: {}", lazy_promise.as_slice(), progress.as_f32()); }
/// }
/// }
/// }
Expand Down Expand Up @@ -85,10 +87,6 @@ impl<T: Debug> LazyVecPromise<T> {

impl<T: Debug> Promise for LazyVecPromise<T> {
fn poll_state(&mut self) -> &DataState {
if self.state == DataState::Uninitialized {
self.update();
}

while let Ok(msg) = self.rx.try_recv() {
match msg {
Message::NewData(data) => {
Expand All @@ -100,6 +98,10 @@ impl<T: Debug> Promise for LazyVecPromise<T> {
}
}

if self.state == DataState::Uninitialized {
self.update();
}

&self.state
}

Expand Down Expand Up @@ -152,13 +154,10 @@ mod test {
assert!(delayed_vec.as_slice().is_empty());
// We have some numbers ready in between
tokio::time::sleep(Duration::from_millis(80)).await;
if let DataState::Updating(progress) = delayed_vec.poll_state() {
assert!(progress.as_f32() > 0.0);
assert!(progress.as_f32() < 1.0);
assert!(!delayed_vec.as_slice().is_empty());
} else {
panic!("Was not in updating state after waiting time");
}
let progress = delayed_vec.poll_state().get_progress().unwrap();
assert!(progress.as_f32() > 0.0);
assert!(progress.as_f32() < 1.0);
assert!(!delayed_vec.as_slice().is_empty());

// after wait we have a result
tokio::time::sleep(Duration::from_millis(200)).await;
Expand Down
18 changes: 17 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ use std::pin::Pin;
use tokio::sync::mpsc::Sender;

/// a f64 type which is constrained to the range of 0.0 and 1.0
#[derive(Clone, PartialEq, Debug)]
#[derive(Clone, Copy, PartialEq, Debug)]
pub struct Progress(f64);

impl<T: Into<f64>> From<T> for Progress {
Expand Down Expand Up @@ -81,6 +81,12 @@ impl Progress {
}
}

impl Default for Progress {
fn default() -> Self {
Progress(0.0)
}
}

#[derive(Clone, PartialEq, Debug)]
/// Represents a processing state.
pub enum DataState {
Expand All @@ -95,6 +101,16 @@ pub enum DataState {
Error(String),
}

impl DataState {
/// Yields the progress if state is `DataState::Updating` otherwise yields none, even if finished.
pub fn get_progress(&self) -> Option<Progress> {
match &self {
DataState::Updating(progress) => Some(*progress),
_ => None,
}
}
}

#[derive(Debug)]
/// The message-type to send from the updater to the main thread. There's only two variants,
/// `NewData` which allows to send new data or `StateChange` which allows to signal readiness or error.
Expand Down

0 comments on commit 73ab695

Please sign in to comment.