From 1a3429655c4f9208891756483e6ca87ba71d08bb Mon Sep 17 00:00:00 2001 From: Yoshua Wuyts Date: Wed, 9 Oct 2019 22:03:28 +0200 Subject: [PATCH] init blocking-updates Signed-off-by: Yoshua Wuyts --- src/task/blocking.rs | 5 +++-- src/task/mod.rs | 8 ++++---- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/task/blocking.rs b/src/task/blocking.rs index 53b52c8..012158d 100644 --- a/src/task/blocking.rs +++ b/src/task/blocking.rs @@ -96,12 +96,13 @@ fn schedule(t: async_task::Task) { /// Spawns a blocking task. /// /// The task will be spawned onto a thread pool specifically dedicated to blocking tasks. -pub(crate) fn spawn(future: F) -> JoinHandle +pub(crate) fn spawn(f: F) -> JoinHandle where - F: Future + Send + 'static, + F: FnOnce() -> R + Send + 'static, R: Send + 'static, { let tag = Tag::new(None); + let future = async move { f() }; let (task, handle) = async_task::spawn(future, schedule, tag); task.schedule(); JoinHandle::new(handle) diff --git a/src/task/mod.rs b/src/task/mod.rs index 727dc59..21b85fd 100644 --- a/src/task/mod.rs +++ b/src/task/mod.rs @@ -73,7 +73,7 @@ cfg_if::cfg_if! { /// # /// use async_std::task; /// -/// task::blocking(async { +/// task::blocking(|| { /// println!("long-running task here"); /// }).await; /// # @@ -84,10 +84,10 @@ cfg_if::cfg_if! { #[cfg(any(feature = "unstable", feature = "docs"))] #[cfg_attr(feature = "docs", doc(cfg(unstable)))] #[inline] -pub fn blocking(future: F) -> task::JoinHandle +pub fn blocking(f: F) -> task::JoinHandle where - F: crate::future::Future + Send + 'static, + F: FnOnce() -> R + Send + 'static, R: Send + 'static, { - blocking::spawn(future) + blocking::spawn_blocking(future) }