async work

This commit is contained in:
Simon Gardling
2022-03-30 10:45:43 -04:00
parent 96d9d3b170
commit e53dab559f
4 changed files with 61 additions and 15 deletions

View File

@@ -2,10 +2,10 @@ use eframe::egui::plot::{Line, Points, Value as EguiValue, Values};
use itertools::Itertools;
use serde_json::Value as JsonValue;
#[cfg(not(target_arch = "wasm32"))]
#[cfg(threading)]
use rayon::prelude::*;
#[cfg(target_arch = "wasm32")]
#[cfg(not(threading))]
pub fn dyn_iter<'a, T>(input: &'a Vec<T>) -> impl Iterator<Item = &'a T>
where
&'a [T]: IntoIterator,
@@ -13,7 +13,7 @@ where
input.iter()
}
#[cfg(not(target_arch = "wasm32"))]
#[cfg(threading)]
pub fn dyn_iter<'a, I>(input: &'a I) -> <&'a I as IntoParallelIterator>::Iter
where
&'a I: IntoParallelIterator,
@@ -21,7 +21,7 @@ where
input.par_iter()
}
#[cfg(target_arch = "wasm32")]
#[cfg(not(threading))]
pub fn dyn_mut_iter<'a, T>(input: &'a mut Vec<T>) -> impl Iterator<Item = &'a mut T>
where
&'a mut [T]: IntoIterator,
@@ -29,7 +29,7 @@ where
input.iter_mut()
}
#[cfg(not(target_arch = "wasm32"))]
#[cfg(threading)]
pub fn dyn_mut_iter<'a, I>(input: &'a mut I) -> <&'a mut I as IntoParallelIterator>::Iter
where
&'a mut I: IntoParallelIterator,
@@ -37,6 +37,35 @@ where
input.par_iter_mut()
}
pub struct FunctionHelper<'a> {
#[cfg(threading)]
f: async_lock::Mutex<Box<dyn Fn(f64, f64) -> f64 + 'a + Sync + Send>>,
#[cfg(not(threading))]
f: Box<dyn Fn(f64, f64) -> f64 + 'a>,
}
impl<'a> FunctionHelper<'a> {
#[cfg(threading)]
pub fn new(f: impl Fn(f64, f64) -> f64 + 'a) -> FunctionHelper<'a> {
FunctionHelper {
f: async_lock::Mutex::new(Box::new(f)),
}
}
pub fn new(f: impl Fn(f64, f64) -> f64 + 'a) -> FunctionHelper<'a> {
FunctionHelper { f: Box::new(f) }
}
// pub fn get(&self, x: f64, x1: f64) -> f64 { (self.f.lock())(x, x1) }
#[cfg(threading)]
pub async fn get(&self, x: f64, x1: f64) -> f64 { (self.f.lock().await)(x, x1) }
#[cfg(not(threading))]
pub fn get(&self, x: f64, x1: f64) -> f64 { (self.f)(x, x1) }
}
pub trait VecValueToTuple {
fn to_tuple(&self) -> Vec<(f64, f64)>;
}