diff --git a/Cargo.toml b/Cargo.toml index cdd2a65..fa42d7d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,5 +25,10 @@ chrono = "0.4" cron = "0.12" thiserror = "1.0" crossbeam-channel = "0.5" -tokio = { version = "1.25", optional = true , features = ["sync", "rt-multi-thread", "time", "macros"] } +tokio = { version = "1.25", optional = true, features = ["sync", "rt-multi-thread", "time", "macros"] } futures = { version = "0.3.26", optional = true } + +[[example]] +name = "async_simple" +path = "examples/async_simple.rs" +required-features = ["async"] diff --git a/src/async_cron.rs b/src/async_cron.rs index 6a6a5df..0afdb77 100644 --- a/src/async_cron.rs +++ b/src/async_cron.rs @@ -53,7 +53,7 @@ where pub async fn add_fn(&mut self, spec: &str, f: F) -> Result where F: 'static + Fn() -> T + Send + Sync, - T: 'static + Future + Send + Sync, + T: 'static + Future + Send, { let schedule = cron::Schedule::from_str(spec)?; self.schedule(schedule, f).await @@ -184,7 +184,7 @@ where async fn schedule(&mut self, schedule: cron::Schedule, f: F) -> Result where F: 'static + Fn() -> T + Send + Sync, - T: 'static + Future + Send + Sync, + T: 'static + Future + Send, { let next_id = self.next_id.fetch_add(1, Ordering::SeqCst); diff --git a/tests/async.rs b/tests/async.rs index f82ba6a..da1871b 100644 --- a/tests/async.rs +++ b/tests/async.rs @@ -1,5 +1,7 @@ +#[cfg(feature = "async")] #[cfg(test)] mod tests { + use std::cell::UnsafeCell; use std::sync::Arc; use chrono::{FixedOffset, Local, TimeZone}; @@ -28,9 +30,14 @@ mod tests { cron.add_fn("* * * * * *", move || { let counter1 = Arc::clone(&counter1); + + // use UnsafeCell to make sure we can still use non-Sync types + let mut inc: UnsafeCell = 1.into(); + async move { let mut value = counter1.lock().await; - *value += 1; + let inc = *inc.get_mut(); + *value += inc; } }) .await