From 1a3429655c4f9208891756483e6ca87ba71d08bb Mon Sep 17 00:00:00 2001 From: Yoshua Wuyts Date: Wed, 9 Oct 2019 22:03:28 +0200 Subject: [PATCH 1/4] 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) } From b4c1c63fd27f3ce6350d9ba11689fe801fb8312e Mon Sep 17 00:00:00 2001 From: Yoshua Wuyts Date: Wed, 9 Oct 2019 22:21:49 +0200 Subject: [PATCH 2/4] task::blocking async closure -> FnOnce Signed-off-by: Yoshua Wuyts --- src/fs/canonicalize.rs | 2 +- src/fs/copy.rs | 2 +- src/fs/create_dir.rs | 2 +- src/fs/create_dir_all.rs | 2 +- src/fs/dir_builder.rs | 2 +- src/fs/dir_entry.rs | 4 ++-- src/fs/file.rs | 20 ++++++++++---------- src/fs/hard_link.rs | 2 +- src/fs/metadata.rs | 2 +- src/fs/open_options.rs | 2 +- src/fs/read.rs | 2 +- src/fs/read_dir.rs | 4 ++-- src/fs/read_link.rs | 2 +- src/fs/read_to_string.rs | 2 +- src/fs/remove_dir.rs | 2 +- src/fs/remove_dir_all.rs | 2 +- src/fs/remove_file.rs | 2 +- src/fs/rename.rs | 2 +- src/fs/set_permissions.rs | 2 +- src/fs/symlink_metadata.rs | 2 +- src/fs/write.rs | 2 +- src/io/stderr.rs | 4 ++-- src/io/stdin.rs | 4 ++-- src/io/stdout.rs | 4 ++-- src/net/addr.rs | 5 ++--- src/net/tcp/stream.rs | 2 +- src/os/unix/fs.rs | 2 +- src/os/unix/net/datagram.rs | 2 +- src/os/unix/net/listener.rs | 2 +- src/os/unix/net/stream.rs | 2 +- src/task/blocking.rs | 1 - 31 files changed, 45 insertions(+), 47 deletions(-) diff --git a/src/fs/canonicalize.rs b/src/fs/canonicalize.rs index a763e4e..601d477 100644 --- a/src/fs/canonicalize.rs +++ b/src/fs/canonicalize.rs @@ -32,5 +32,5 @@ use crate::task::blocking; /// ``` pub async fn canonicalize>(path: P) -> io::Result { let path = path.as_ref().to_owned(); - blocking::spawn(async move { std::fs::canonicalize(&path).map(Into::into) }).await + blocking::spawn(move || std::fs::canonicalize(&path).map(Into::into)).await } diff --git a/src/fs/copy.rs b/src/fs/copy.rs index c0c6b9e..733fb64 100644 --- a/src/fs/copy.rs +++ b/src/fs/copy.rs @@ -41,5 +41,5 @@ use crate::task::blocking; pub async fn copy, Q: AsRef>(from: P, to: Q) -> io::Result { let from = from.as_ref().to_owned(); let to = to.as_ref().to_owned(); - blocking::spawn(async move { std::fs::copy(&from, &to) }).await + blocking::spawn(move || std::fs::copy(&from, &to)).await } diff --git a/src/fs/create_dir.rs b/src/fs/create_dir.rs index 99f4ac4..740d303 100644 --- a/src/fs/create_dir.rs +++ b/src/fs/create_dir.rs @@ -34,5 +34,5 @@ use crate::task::blocking; /// ``` pub async fn create_dir>(path: P) -> io::Result<()> { let path = path.as_ref().to_owned(); - blocking::spawn(async move { std::fs::create_dir(path) }).await + blocking::spawn(move || std::fs::create_dir(path)).await } diff --git a/src/fs/create_dir_all.rs b/src/fs/create_dir_all.rs index 0dc446e..76604de 100644 --- a/src/fs/create_dir_all.rs +++ b/src/fs/create_dir_all.rs @@ -29,5 +29,5 @@ use crate::task::blocking; /// ``` pub async fn create_dir_all>(path: P) -> io::Result<()> { let path = path.as_ref().to_owned(); - blocking::spawn(async move { std::fs::create_dir_all(path) }).await + blocking::spawn(move || std::fs::create_dir_all(path)).await } diff --git a/src/fs/dir_builder.rs b/src/fs/dir_builder.rs index 1fb0850..738ddbc 100644 --- a/src/fs/dir_builder.rs +++ b/src/fs/dir_builder.rs @@ -108,7 +108,7 @@ impl DirBuilder { } let path = path.as_ref().to_owned(); - async move { blocking::spawn(async move { builder.create(path) }).await } + async move { blocking::spawn(move || builder.create(path)).await } } } diff --git a/src/fs/dir_entry.rs b/src/fs/dir_entry.rs index 3d42f83..80d4bbd 100644 --- a/src/fs/dir_entry.rs +++ b/src/fs/dir_entry.rs @@ -89,7 +89,7 @@ impl DirEntry { /// ``` pub async fn metadata(&self) -> io::Result { let inner = self.0.clone(); - blocking::spawn(async move { inner.metadata() }).await + blocking::spawn(move || inner.metadata()).await } /// Reads the file type for this entry. @@ -127,7 +127,7 @@ impl DirEntry { /// ``` pub async fn file_type(&self) -> io::Result { let inner = self.0.clone(); - blocking::spawn(async move { inner.file_type() }).await + blocking::spawn(move || inner.file_type()).await } /// Returns the bare name of this entry without the leading path. diff --git a/src/fs/file.rs b/src/fs/file.rs index b52bcd0..3916234 100644 --- a/src/fs/file.rs +++ b/src/fs/file.rs @@ -97,7 +97,7 @@ impl File { /// ``` pub async fn open>(path: P) -> io::Result { let path = path.as_ref().to_owned(); - let file = blocking::spawn(async move { std::fs::File::open(&path) }).await?; + let file = blocking::spawn(move || std::fs::File::open(&path)).await?; Ok(file.into()) } @@ -132,7 +132,7 @@ impl File { /// ``` pub async fn create>(path: P) -> io::Result { let path = path.as_ref().to_owned(); - let file = blocking::spawn(async move { std::fs::File::create(&path) }).await?; + let file = blocking::spawn(move || std::fs::File::create(&path)).await?; Ok(file.into()) } @@ -165,7 +165,7 @@ impl File { }) .await?; - blocking::spawn(async move { state.file.sync_all() }).await + blocking::spawn(move || state.file.sync_all()).await } /// Synchronizes OS-internal buffered contents to disk. @@ -201,7 +201,7 @@ impl File { }) .await?; - blocking::spawn(async move { state.file.sync_data() }).await + blocking::spawn(move || state.file.sync_data()).await } /// Truncates or extends the file. @@ -234,7 +234,7 @@ impl File { }) .await?; - blocking::spawn(async move { state.file.set_len(size) }).await + blocking::spawn(move || state.file.set_len(size)).await } /// Reads the file's metadata. @@ -253,7 +253,7 @@ impl File { /// ``` pub async fn metadata(&self) -> io::Result { let file = self.file.clone(); - blocking::spawn(async move { file.metadata() }).await + blocking::spawn(move || file.metadata()).await } /// Changes the permissions on the file. @@ -282,7 +282,7 @@ impl File { /// ``` pub async fn set_permissions(&self, perm: Permissions) -> io::Result<()> { let file = self.file.clone(); - blocking::spawn(async move { file.set_permissions(perm) }).await + blocking::spawn(move || file.set_permissions(perm)).await } } @@ -702,7 +702,7 @@ impl LockGuard { self.register(cx); // Start a read operation asynchronously. - blocking::spawn(async move { + blocking::spawn(move || { // Read some data from the file into the cache. let res = { let State { file, cache, .. } = &mut *self; @@ -811,7 +811,7 @@ impl LockGuard { self.register(cx); // Start a write operation asynchronously. - blocking::spawn(async move { + blocking::spawn(move || { match (&*self.file).write_all(&self.cache) { Ok(_) => { // Switch to idle mode. @@ -844,7 +844,7 @@ impl LockGuard { self.register(cx); // Start a flush operation asynchronously. - blocking::spawn(async move { + blocking::spawn(move || { match (&*self.file).flush() { Ok(()) => { // Mark the file as flushed. diff --git a/src/fs/hard_link.rs b/src/fs/hard_link.rs index 2ae2cad..8b09b5d 100644 --- a/src/fs/hard_link.rs +++ b/src/fs/hard_link.rs @@ -32,5 +32,5 @@ use crate::task::blocking; pub async fn hard_link, Q: AsRef>(from: P, to: Q) -> io::Result<()> { let from = from.as_ref().to_owned(); let to = to.as_ref().to_owned(); - blocking::spawn(async move { std::fs::hard_link(&from, &to) }).await + blocking::spawn(move || std::fs::hard_link(&from, &to)).await } diff --git a/src/fs/metadata.rs b/src/fs/metadata.rs index 8c92381..65f494b 100644 --- a/src/fs/metadata.rs +++ b/src/fs/metadata.rs @@ -36,7 +36,7 @@ use crate::task::blocking; /// ``` pub async fn metadata>(path: P) -> io::Result { let path = path.as_ref().to_owned(); - blocking::spawn(async move { std::fs::metadata(path) }).await + blocking::spawn(move || std::fs::metadata(path)).await } cfg_if! { diff --git a/src/fs/open_options.rs b/src/fs/open_options.rs index d7e0454..a9511f1 100644 --- a/src/fs/open_options.rs +++ b/src/fs/open_options.rs @@ -285,7 +285,7 @@ impl OpenOptions { pub fn open>(&self, path: P) -> impl Future> { let path = path.as_ref().to_owned(); let options = self.0.clone(); - async move { blocking::spawn(async move { options.open(path).map(|f| f.into()) }).await } + async move { blocking::spawn(move || options.open(path).map(|f| f.into())).await } } } diff --git a/src/fs/read.rs b/src/fs/read.rs index b562ea3..a0eb130 100644 --- a/src/fs/read.rs +++ b/src/fs/read.rs @@ -36,5 +36,5 @@ use crate::task::blocking; /// ``` pub async fn read>(path: P) -> io::Result> { let path = path.as_ref().to_owned(); - blocking::spawn(async move { std::fs::read(path) }).await + blocking::spawn(move || std::fs::read(path)).await } diff --git a/src/fs/read_dir.rs b/src/fs/read_dir.rs index fd1a21f..6e47801 100644 --- a/src/fs/read_dir.rs +++ b/src/fs/read_dir.rs @@ -45,7 +45,7 @@ use crate::task::{blocking, Context, JoinHandle, Poll}; /// ``` pub async fn read_dir>(path: P) -> io::Result { let path = path.as_ref().to_owned(); - blocking::spawn(async move { std::fs::read_dir(path) }) + blocking::spawn(move || std::fs::read_dir(path)) .await .map(ReadDir::new) } @@ -91,7 +91,7 @@ impl Stream for ReadDir { let mut inner = opt.take().unwrap(); // Start the operation asynchronously. - self.0 = State::Busy(blocking::spawn(async move { + self.0 = State::Busy(blocking::spawn(move || { let next = inner.next(); (inner, next) })); diff --git a/src/fs/read_link.rs b/src/fs/read_link.rs index 53080dd..eaa7b62 100644 --- a/src/fs/read_link.rs +++ b/src/fs/read_link.rs @@ -28,5 +28,5 @@ use crate::task::blocking; /// ``` pub async fn read_link>(path: P) -> io::Result { let path = path.as_ref().to_owned(); - blocking::spawn(async move { std::fs::read_link(path).map(Into::into) }).await + blocking::spawn(move || std::fs::read_link(path).map(Into::into)).await } diff --git a/src/fs/read_to_string.rs b/src/fs/read_to_string.rs index a4d175f..40c4b6b 100644 --- a/src/fs/read_to_string.rs +++ b/src/fs/read_to_string.rs @@ -37,5 +37,5 @@ use crate::task::blocking; /// ``` pub async fn read_to_string>(path: P) -> io::Result { let path = path.as_ref().to_owned(); - blocking::spawn(async move { std::fs::read_to_string(path) }).await + blocking::spawn(move || std::fs::read_to_string(path)).await } diff --git a/src/fs/remove_dir.rs b/src/fs/remove_dir.rs index f457124..d1fa7bf 100644 --- a/src/fs/remove_dir.rs +++ b/src/fs/remove_dir.rs @@ -29,5 +29,5 @@ use crate::task::blocking; /// ``` pub async fn remove_dir>(path: P) -> io::Result<()> { let path = path.as_ref().to_owned(); - blocking::spawn(async move { std::fs::remove_dir(path) }).await + blocking::spawn(move || std::fs::remove_dir(path)).await } diff --git a/src/fs/remove_dir_all.rs b/src/fs/remove_dir_all.rs index 3b12d26..0a0fceb 100644 --- a/src/fs/remove_dir_all.rs +++ b/src/fs/remove_dir_all.rs @@ -29,5 +29,5 @@ use crate::task::blocking; /// ``` pub async fn remove_dir_all>(path: P) -> io::Result<()> { let path = path.as_ref().to_owned(); - blocking::spawn(async move { std::fs::remove_dir_all(path) }).await + blocking::spawn(move || std::fs::remove_dir_all(path)).await } diff --git a/src/fs/remove_file.rs b/src/fs/remove_file.rs index 216209f..5bc0608 100644 --- a/src/fs/remove_file.rs +++ b/src/fs/remove_file.rs @@ -29,5 +29,5 @@ use crate::task::blocking; /// ``` pub async fn remove_file>(path: P) -> io::Result<()> { let path = path.as_ref().to_owned(); - blocking::spawn(async move { std::fs::remove_file(path) }).await + blocking::spawn(move || std::fs::remove_file(path)).await } diff --git a/src/fs/rename.rs b/src/fs/rename.rs index f517a26..c2aa77b 100644 --- a/src/fs/rename.rs +++ b/src/fs/rename.rs @@ -34,5 +34,5 @@ use crate::task::blocking; pub async fn rename, Q: AsRef>(from: P, to: Q) -> io::Result<()> { let from = from.as_ref().to_owned(); let to = to.as_ref().to_owned(); - blocking::spawn(async move { std::fs::rename(&from, &to) }).await + blocking::spawn(move || std::fs::rename(&from, &to)).await } diff --git a/src/fs/set_permissions.rs b/src/fs/set_permissions.rs index 68dd8d4..d14ced9 100644 --- a/src/fs/set_permissions.rs +++ b/src/fs/set_permissions.rs @@ -32,5 +32,5 @@ use crate::task::blocking; /// ``` pub async fn set_permissions>(path: P, perm: Permissions) -> io::Result<()> { let path = path.as_ref().to_owned(); - blocking::spawn(async move { std::fs::set_permissions(path, perm) }).await + blocking::spawn(move || std::fs::set_permissions(path, perm)).await } diff --git a/src/fs/symlink_metadata.rs b/src/fs/symlink_metadata.rs index e2bc12d..bc5cce8 100644 --- a/src/fs/symlink_metadata.rs +++ b/src/fs/symlink_metadata.rs @@ -34,5 +34,5 @@ use crate::task::blocking; /// ``` pub async fn symlink_metadata>(path: P) -> io::Result { let path = path.as_ref().to_owned(); - blocking::spawn(async move { std::fs::symlink_metadata(path) }).await + blocking::spawn(move || std::fs::symlink_metadata(path)).await } diff --git a/src/fs/write.rs b/src/fs/write.rs index 4db5221..3df5604 100644 --- a/src/fs/write.rs +++ b/src/fs/write.rs @@ -33,5 +33,5 @@ use crate::task::blocking; pub async fn write, C: AsRef<[u8]>>(path: P, contents: C) -> io::Result<()> { let path = path.as_ref().to_owned(); let contents = contents.as_ref().to_owned(); - blocking::spawn(async move { std::fs::write(path, contents) }).await + blocking::spawn(move || std::fs::write(path, contents)).await } diff --git a/src/io/stderr.rs b/src/io/stderr.rs index 5706aa2..34d4d1d 100644 --- a/src/io/stderr.rs +++ b/src/io/stderr.rs @@ -116,7 +116,7 @@ impl Write for Stderr { inner.buf[..buf.len()].copy_from_slice(buf); // Start the operation asynchronously. - *state = State::Busy(blocking::spawn(async move { + *state = State::Busy(blocking::spawn(move || { let res = std::io::Write::write(&mut inner.stderr, &inner.buf); inner.last_op = Some(Operation::Write(res)); State::Idle(Some(inner)) @@ -144,7 +144,7 @@ impl Write for Stderr { let mut inner = opt.take().unwrap(); // Start the operation asynchronously. - *state = State::Busy(blocking::spawn(async move { + *state = State::Busy(blocking::spawn(move || { let res = std::io::Write::flush(&mut inner.stderr); inner.last_op = Some(Operation::Flush(res)); State::Idle(Some(inner)) diff --git a/src/io/stdin.rs b/src/io/stdin.rs index 95a77b8..178e819 100644 --- a/src/io/stdin.rs +++ b/src/io/stdin.rs @@ -119,7 +119,7 @@ impl Stdin { let mut inner = opt.take().unwrap(); // Start the operation asynchronously. - *state = State::Busy(blocking::spawn(async move { + *state = State::Busy(blocking::spawn(move || { inner.line.clear(); let res = inner.stdin.read_line(&mut inner.line); inner.last_op = Some(Operation::ReadLine(res)); @@ -172,7 +172,7 @@ impl Read for Stdin { } // Start the operation asynchronously. - *state = State::Busy(blocking::spawn(async move { + *state = State::Busy(blocking::spawn(move || { let res = std::io::Read::read(&mut inner.stdin, &mut inner.buf); inner.last_op = Some(Operation::Read(res)); State::Idle(Some(inner)) diff --git a/src/io/stdout.rs b/src/io/stdout.rs index 7849f1c..4128aae 100644 --- a/src/io/stdout.rs +++ b/src/io/stdout.rs @@ -116,7 +116,7 @@ impl Write for Stdout { inner.buf[..buf.len()].copy_from_slice(buf); // Start the operation asynchronously. - *state = State::Busy(blocking::spawn(async move { + *state = State::Busy(blocking::spawn(move || { let res = std::io::Write::write(&mut inner.stdout, &inner.buf); inner.last_op = Some(Operation::Write(res)); State::Idle(Some(inner)) @@ -144,7 +144,7 @@ impl Write for Stdout { let mut inner = opt.take().unwrap(); // Start the operation asynchronously. - *state = State::Busy(blocking::spawn(async move { + *state = State::Busy(blocking::spawn(move || { let res = std::io::Write::flush(&mut inner.stdout); inner.last_op = Some(Operation::Flush(res)); State::Idle(Some(inner)) diff --git a/src/net/addr.rs b/src/net/addr.rs index 64b22cf..adc2408 100644 --- a/src/net/addr.rs +++ b/src/net/addr.rs @@ -196,7 +196,7 @@ impl ToSocketAddrs for (&str, u16) { } let host = host.to_string(); - let task = blocking::spawn(async move { + let task = blocking::spawn(move || { std::net::ToSocketAddrs::to_socket_addrs(&(host.as_str(), port)) }); ToSocketAddrsFuture::Resolving(task) @@ -217,8 +217,7 @@ impl ToSocketAddrs for str { } let addr = self.to_string(); - let task = - blocking::spawn(async move { std::net::ToSocketAddrs::to_socket_addrs(addr.as_str()) }); + let task = blocking::spawn(move || std::net::ToSocketAddrs::to_socket_addrs(addr.as_str())); ToSocketAddrsFuture::Resolving(task) } } diff --git a/src/net/tcp/stream.rs b/src/net/tcp/stream.rs index 1056943..1ffd636 100644 --- a/src/net/tcp/stream.rs +++ b/src/net/tcp/stream.rs @@ -76,7 +76,7 @@ impl TcpStream { let mut last_err = None; for addr in addrs.to_socket_addrs().await? { - let res = blocking::spawn(async move { + let res = blocking::spawn(move || { let std_stream = std::net::TcpStream::connect(addr)?; let mio_stream = mio::net::TcpStream::from_stream(std_stream)?; Ok(TcpStream { diff --git a/src/os/unix/fs.rs b/src/os/unix/fs.rs index f00aaec..4a15510 100644 --- a/src/os/unix/fs.rs +++ b/src/os/unix/fs.rs @@ -28,7 +28,7 @@ use crate::task::blocking; pub async fn symlink, Q: AsRef>(src: P, dst: Q) -> io::Result<()> { let src = src.as_ref().to_owned(); let dst = dst.as_ref().to_owned(); - blocking::spawn(async move { std::os::unix::fs::symlink(&src, &dst) }).await + blocking::spawn(move || std::os::unix::fs::symlink(&src, &dst)).await } cfg_if! { diff --git a/src/os/unix/net/datagram.rs b/src/os/unix/net/datagram.rs index 61adc09..c96afd5 100644 --- a/src/os/unix/net/datagram.rs +++ b/src/os/unix/net/datagram.rs @@ -67,7 +67,7 @@ impl UnixDatagram { /// ``` pub async fn bind>(path: P) -> io::Result { let path = path.as_ref().to_owned(); - let socket = blocking::spawn(async move { mio_uds::UnixDatagram::bind(path) }).await?; + let socket = blocking::spawn(move || mio_uds::UnixDatagram::bind(path)).await?; Ok(UnixDatagram::new(socket)) } diff --git a/src/os/unix/net/listener.rs b/src/os/unix/net/listener.rs index 2d68a6b..b6e6a29 100644 --- a/src/os/unix/net/listener.rs +++ b/src/os/unix/net/listener.rs @@ -68,7 +68,7 @@ impl UnixListener { /// ``` pub async fn bind>(path: P) -> io::Result { let path = path.as_ref().to_owned(); - let listener = blocking::spawn(async move { mio_uds::UnixListener::bind(path) }).await?; + let listener = blocking::spawn(move || mio_uds::UnixListener::bind(path)).await?; Ok(UnixListener { watcher: Watcher::new(listener), diff --git a/src/os/unix/net/stream.rs b/src/os/unix/net/stream.rs index 8245e63..b16f2a3 100644 --- a/src/os/unix/net/stream.rs +++ b/src/os/unix/net/stream.rs @@ -58,7 +58,7 @@ impl UnixStream { pub async fn connect>(path: P) -> io::Result { let path = path.as_ref().to_owned(); - blocking::spawn(async move { + blocking::spawn(move || { let std_stream = std::os::unix::net::UnixStream::connect(path)?; let mio_stream = mio_uds::UnixStream::from_stream(std_stream)?; Ok(UnixStream { diff --git a/src/task/blocking.rs b/src/task/blocking.rs index 012158d..3216012 100644 --- a/src/task/blocking.rs +++ b/src/task/blocking.rs @@ -7,7 +7,6 @@ use std::time::Duration; use crossbeam_channel::{bounded, Receiver, Sender}; use lazy_static::lazy_static; -use crate::future::Future; use crate::task::task::{JoinHandle, Tag}; use crate::utils::abort_on_panic; From 12fdc1232ddd83f43e46659eca95229cc346873f Mon Sep 17 00:00:00 2001 From: Yoshua Wuyts Date: Wed, 9 Oct 2019 22:24:38 +0200 Subject: [PATCH 3/4] rename task::blocking to task::spawn_blocking Signed-off-by: Yoshua Wuyts --- src/task/block_on.rs | 2 +- src/task/mod.rs | 9 +++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/task/block_on.rs b/src/task/block_on.rs index 31588c4..e1b6d02 100644 --- a/src/task/block_on.rs +++ b/src/task/block_on.rs @@ -21,7 +21,7 @@ use kv_log_macro::trace; /// /// See also: [`task::blocking`]. /// -/// [`task::blocking`]: fn.blocking.html +/// [`task::spawn_blocking`]: fn.spawn_blocking.html /// /// [spawning]: https://doc.rust-lang.org/std/thread/fn.spawn.html /// [joining]: https://doc.rust-lang.org/std/thread/struct.JoinHandle.html#method.join diff --git a/src/task/mod.rs b/src/task/mod.rs index 21b85fd..fc702df 100644 --- a/src/task/mod.rs +++ b/src/task/mod.rs @@ -60,9 +60,10 @@ cfg_if::cfg_if! { /// is useful to prevent long-running synchronous operations from blocking the main futures /// executor. /// -/// See also: [`task::block_on`]. +/// See also: [`task::block_on`], [`task::spawn`]. /// /// [`task::block_on`]: fn.block_on.html +/// [`task::spawn`]: fn.spawn.html /// /// # Examples /// @@ -73,7 +74,7 @@ cfg_if::cfg_if! { /// # /// use async_std::task; /// -/// task::blocking(|| { +/// task::spawn_blocking(|| { /// println!("long-running task here"); /// }).await; /// # @@ -84,10 +85,10 @@ cfg_if::cfg_if! { #[cfg(any(feature = "unstable", feature = "docs"))] #[cfg_attr(feature = "docs", doc(cfg(unstable)))] #[inline] -pub fn blocking(f: F) -> task::JoinHandle +pub fn spawn_blocking(f: F) -> task::JoinHandle where F: FnOnce() -> R + Send + 'static, R: Send + 'static, { - blocking::spawn_blocking(future) + blocking::spawn(f) } From 33806ad44ce7a80ae49dd2da57f60ad9680d4293 Mon Sep 17 00:00:00 2001 From: Yoshua Wuyts Date: Wed, 9 Oct 2019 23:44:26 +0200 Subject: [PATCH 4/4] fix warning Signed-off-by: Yoshua Wuyts --- src/task/block_on.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/task/block_on.rs b/src/task/block_on.rs index e1b6d02..db46f02 100644 --- a/src/task/block_on.rs +++ b/src/task/block_on.rs @@ -19,7 +19,7 @@ use kv_log_macro::trace; /// Calling this function is similar to [spawning] a thread and immediately [joining] it, except an /// asynchronous task will be spawned. /// -/// See also: [`task::blocking`]. +/// See also: [`task::spawn_blocking`]. /// /// [`task::spawn_blocking`]: fn.spawn_blocking.html ///