Skip to content

Commit

Permalink
Merge pull request #6 from ChrisRega/0.3.1-dev
Browse files Browse the repository at this point in the history
0.3.1 dev
  • Loading branch information
ChrisRega authored Nov 10, 2022
2 parents 73ab695 + 973a99f commit bc2cda8
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 33 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ Example-usage of this crate with a small egui/eframe blog-reader can be found [h

Changelog:

0.3.1:
- Add better api macros for lazy structures

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`
Expand Down
20 changes: 7 additions & 13 deletions src/lazyvalue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,14 @@ use tokio::sync::mpsc::{channel, Receiver, Sender};
/// ```rust, no_run
/// use std::time::Duration;
/// use tokio::sync::mpsc::Sender;
/// use lazy_async_promise::{DataState, Message, Promise};
/// use lazy_async_promise::LazyValuePromise;
/// use lazy_async_promise::unpack_result;
/// use lazy_async_promise::{DataState, Message, Promise, LazyValuePromise, api_macros::*};
/// // updater-future:
/// let updater = |tx: Sender<Message<i32>>| async move {
/// tx.send(Message::NewData(1337)).await.unwrap();
/// 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;
/// tx.send(Message::StateChange(DataState::UpToDate)).await.unwrap();
/// set_finished!(tx);
/// };
/// // direct usage:
/// let promise = LazyValuePromise::new(updater, 10);
Expand Down Expand Up @@ -111,7 +109,7 @@ impl<T: Debug> Promise for LazyValuePromise<T> {
#[cfg(test)]
mod test {
use super::*;
use crate::unpack_result;
use crate::api_macros::*;
use std::time::Duration;
use tokio::runtime::{Builder, Runtime};
use tokio::sync::mpsc::Sender;
Expand All @@ -120,12 +118,10 @@ mod test {
fn basic_usage_cycle() {
let string_maker = |tx: Sender<Message<String>>| async move {
for i in 0..2 {
tx.send(Message::NewData(i.to_string())).await.unwrap();
send_data!(i.to_string(), tx);
tokio::time::sleep(Duration::from_millis(20)).await;
}
tx.send(Message::StateChange(DataState::UpToDate))
.await
.unwrap();
set_finished!(tx);
};

Builder::new_multi_thread()
Expand Down Expand Up @@ -166,9 +162,7 @@ mod test {
fn error_propagation() {
let error_maker = |tx: Sender<Message<String>>| async move {
let _ = unpack_result!(std::fs::read_to_string("FILE_NOT_EXISTING"), tx);
tx.send(Message::StateChange(DataState::UpToDate))
.await
.unwrap();
unreachable!();
};

Runtime::new().unwrap().block_on(async {
Expand Down
25 changes: 9 additions & 16 deletions src/lazyvec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,21 @@ use tokio::sync::mpsc::{channel, Receiver, Sender};
/// ```rust, no_run
/// use std::time::Duration;
/// use tokio::sync::mpsc::Sender;
/// use lazy_async_promise::{DataState, Message, Progress, Promise, set_progress};
/// use lazy_async_promise::LazyVecPromise;
/// use lazy_async_promise::unpack_result;
/// use lazy_async_promise::{DataState, Message, Promise, LazyVecPromise, api_macros::*};
/// // updater-future:
/// let updater = |tx: Sender<Message<i32>>| async move {
/// const ITEM_COUNT: i32 = 100;
/// for i in 0..ITEM_COUNT {
/// tx.send(Message::NewData(i)).await.unwrap();
/// 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 {
/// tx.send(Message::StateChange(DataState::Error("loop overflow".to_owned()))).await.unwrap();
/// set_error!("loop overflow".to_owned(), tx);
/// }
/// tokio::time::sleep(Duration::from_millis(100)).await;
/// }
/// tx.send(Message::StateChange(DataState::UpToDate)).await.unwrap();
/// set_finished!(tx);
/// };
/// // direct usage:
/// let promise = LazyVecPromise::new(updater, 200);
Expand Down Expand Up @@ -120,7 +118,7 @@ impl<T: Debug> Promise for LazyVecPromise<T> {
#[cfg(test)]
mod test {
use super::*;
use crate::{set_progress, unpack_result, Progress};
use crate::api_macros::*;
use std::time::Duration;
use tokio::runtime::{Builder, Runtime};
use tokio::sync::mpsc::Sender;
Expand All @@ -130,13 +128,11 @@ mod test {
let string_maker = |tx: Sender<Message<String>>| async move {
const COUNT: i32 = 5;
for i in 0..COUNT {
tx.send(Message::NewData(i.to_string())).await.unwrap();
send_data!(i.to_string(), tx);
set_progress!(Progress::from_fraction(i, COUNT), tx);
tokio::time::sleep(Duration::from_millis(20)).await;
}
tx.send(Message::StateChange(DataState::UpToDate))
.await
.unwrap();
set_finished!(tx);
};

Builder::new_multi_thread()
Expand All @@ -150,7 +146,6 @@ mod test {
// start empty, polling triggers update
assert!(delayed_vec.is_uninitialized());
assert_eq!(*delayed_vec.poll_state(), DataState::Updating(0.0.into()));
println!("Slice is: {:?}", delayed_vec.as_slice());
assert!(delayed_vec.as_slice().is_empty());
// We have some numbers ready in between
tokio::time::sleep(Duration::from_millis(80)).await;
Expand All @@ -175,12 +170,10 @@ mod test {
}

#[test]
fn error_propagation() {
fn error_propagation_returns_early() {
let error_maker = |tx: Sender<Message<String>>| async move {
let _ = unpack_result!(std::fs::read_to_string("NOT_EXISTING"), tx);
tx.send(Message::StateChange(DataState::UpToDate))
.await
.unwrap();
unreachable!();
};

Runtime::new().unwrap().block_on(async {
Expand Down
45 changes: 41 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@ impl<T: Into<f64>> From<T> for Progress {
}
}

/// Use this to get all macros
pub mod api_macros {
pub use crate::send_data;
pub use crate::set_error;
pub use crate::set_finished;
pub use crate::set_progress;
pub use crate::unpack_result;
pub use crate::Progress;
}

impl Progress {
/// Create a Progress from a percentage
/// ```rust, no_run
Expand Down Expand Up @@ -138,10 +148,7 @@ macro_rules! unpack_result {
match $result {
Ok(val) => val,
Err(e) => {
$sender
.send(Message::StateChange(DataState::Error(format!("{}", e))))
.await
.unwrap();
set_error!(format!("{}", e), $sender);
return;
}
}
Expand All @@ -159,6 +166,36 @@ macro_rules! set_progress {
};
}

#[macro_export]
/// Setting the given progress using a given sender.
macro_rules! set_error {
($error: expr, $sender: expr) => {
$sender
.send(Message::StateChange(DataState::Error($error)))
.await
.unwrap();
};
}

#[macro_export]
/// Send new data via the sender
macro_rules! send_data {
($data: expr, $sender: expr) => {
$sender.send(Message::NewData($data)).await.unwrap();
};
}

#[macro_export]
/// Set state to `DataState::UpToDate`
macro_rules! set_finished {
($sender: expr) => {
$sender
.send(Message::StateChange(DataState::UpToDate))
.await
.unwrap();
};
}

type BoxedFutureFactory<T> =
Box<dyn Fn(Sender<Message<T>>) -> Pin<Box<dyn Future<Output = ()> + Send + 'static>>>;

Expand Down

0 comments on commit bc2cda8

Please sign in to comment.