From dcebded8a3cb5428ec9d4416e5c56f4f6c3f7034 Mon Sep 17 00:00:00 2001 From: JMARyA Date: Tue, 1 Apr 2025 13:28:04 +0200 Subject: [PATCH 1/2] =?UTF-8?q?=F0=9F=9A=91=EF=B8=8F=20rally=20fn=20fix?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/lib.rs | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 12ee43f..75d91aa 100644 --- a/src/lib.rs +++ b/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. pub fn rally(items: Vec, f: F) -> (T, X) where - F: Fn(&T) -> X + Send + Sync + Copy + 'static, + F: Fn(&T) -> Option + Send + Sync + Copy + 'static, { + let item_len = items.len(); + let (tx, rx) = mpsc::channel(); let items_len = items.len(); let mut handles = Vec::new(); @@ -51,16 +53,25 @@ where drop(tx); - let (fastest_item, fastest_result, elapsed) = rx.recv().unwrap(); + let mut count = 0; - for handle in handles { - // todo : threads do not get killed here - handle.thread().unpark(); + while count < item_len { + let (fastest_item, fastest_result, elapsed) = rx.recv().unwrap(); + count += 1; + + if fastest_result.is_some() { + for handle in handles { + // todo : threads do not get killed here + handle.thread().unpark(); + } + + log::info!("Rally ended with {items_len} items in {elapsed:?}"); + + return (fastest_item, fastest_result.unwrap()); + } } - log::info!("Rally ended with {items_len} items in {elapsed:?}"); - - (fastest_item, fastest_result) + panic!("No useable results in rally") } pub fn retry Option>(f: F) -> O { From 10e5c7745412f86e5df3a89c037c53abf6b2a1ad Mon Sep 17 00:00:00 2001 From: JMARyA Date: Sun, 27 Apr 2025 19:26:34 +0200 Subject: [PATCH 2/2] update --- examples/functions.rs | 5 +++-- src/lib.rs | 6 +++--- src/service.rs | 2 -- 3 files changed, 6 insertions(+), 7 deletions(-) diff --git a/examples/functions.rs b/examples/functions.rs index 7783239..a4b4a7d 100644 --- a/examples/functions.rs +++ b/examples/functions.rs @@ -9,8 +9,9 @@ fn main() { let (input, output) = rally(items, |item: &_| { std::thread::sleep(Duration::from_millis(item * 100)); - return 0; - }); + return Some(0); + }) + .unwrap(); println!("RALLY RESULTS: {input:?} -> {output:?}"); } diff --git a/src/lib.rs b/src/lib.rs index 75d91aa..293521c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -27,7 +27,7 @@ pub static UNION: Lazy< /// Rally Function /// /// 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(items: Vec, f: F) -> (T, X) +pub fn rally(items: Vec, f: F) -> Option<(T, X)> where F: Fn(&T) -> Option + Send + Sync + Copy + 'static, { @@ -67,11 +67,11 @@ where log::info!("Rally ended with {items_len} items in {elapsed:?}"); - return (fastest_item, fastest_result.unwrap()); + return Some((fastest_item, fastest_result.unwrap())); } } - panic!("No useable results in rally") + None } pub fn retry Option>(f: F) -> O { diff --git a/src/service.rs b/src/service.rs index 9cf3f37..309dc51 100644 --- a/src/service.rs +++ b/src/service.rs @@ -61,8 +61,6 @@ pub struct ServiceManager { pub mode: ServiceMode, } -// TODO : impl decay mode - /// The mode on which services should operate pub enum ServiceMode { /// Behave like a daemon. Services can never die and will come back to life after beeing killed. They will always haunt you.