🚑️ rally fn fix
This commit is contained in:
parent
4e49080958
commit
dcebded8a3
1 changed files with 19 additions and 8 deletions
17
src/lib.rs
17
src/lib.rs
|
@ -29,8 +29,10 @@ pub static UNION: Lazy<
|
||||||
/// This executes a thread for every item executing `f(&item) -> X`. Whatever function returns first is returned while every other thread is killed.
|
/// This executes a thread for every item executing `f(&item) -> X`. Whatever function returns first is returned while every other thread is killed.
|
||||||
pub fn rally<T: Send + Sync + 'static, F, X: Send + 'static>(items: Vec<T>, f: F) -> (T, X)
|
pub fn rally<T: Send + Sync + 'static, F, X: Send + 'static>(items: Vec<T>, f: F) -> (T, X)
|
||||||
where
|
where
|
||||||
F: Fn(&T) -> X + Send + Sync + Copy + 'static,
|
F: Fn(&T) -> Option<X> + Send + Sync + Copy + 'static,
|
||||||
{
|
{
|
||||||
|
let item_len = items.len();
|
||||||
|
|
||||||
let (tx, rx) = mpsc::channel();
|
let (tx, rx) = mpsc::channel();
|
||||||
let items_len = items.len();
|
let items_len = items.len();
|
||||||
let mut handles = Vec::new();
|
let mut handles = Vec::new();
|
||||||
|
@ -51,8 +53,13 @@ where
|
||||||
|
|
||||||
drop(tx);
|
drop(tx);
|
||||||
|
|
||||||
let (fastest_item, fastest_result, elapsed) = rx.recv().unwrap();
|
let mut count = 0;
|
||||||
|
|
||||||
|
while count < item_len {
|
||||||
|
let (fastest_item, fastest_result, elapsed) = rx.recv().unwrap();
|
||||||
|
count += 1;
|
||||||
|
|
||||||
|
if fastest_result.is_some() {
|
||||||
for handle in handles {
|
for handle in handles {
|
||||||
// todo : threads do not get killed here
|
// todo : threads do not get killed here
|
||||||
handle.thread().unpark();
|
handle.thread().unpark();
|
||||||
|
@ -60,7 +67,11 @@ where
|
||||||
|
|
||||||
log::info!("Rally ended with {items_len} items in {elapsed:?}");
|
log::info!("Rally ended with {items_len} items in {elapsed:?}");
|
||||||
|
|
||||||
(fastest_item, fastest_result)
|
return (fastest_item, fastest_result.unwrap());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
panic!("No useable results in rally")
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn retry<O, F: Fn() -> Option<O>>(f: F) -> O {
|
pub fn retry<O, F: Fn() -> Option<O>>(f: F) -> O {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue