From b05fa450c7f2814962f47b2e45f5319c99de58e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1niel=20Buga?= Date: Fri, 29 Jan 2021 15:55:37 +0100 Subject: [PATCH 1/5] Docs: fix link to io --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 9a1c00c1..ebbb3965 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -111,7 +111,7 @@ //! [files]: fs/struct.File.html //! [TCP]: net/struct.TcpStream.html //! [UDP]: net/struct.UdpSocket.html -//! [`io`]: fs/struct.File.html +//! [`io`]: io/index.html //! [`sync`]: sync/index.html //! [`channel`]: channel/index.html //! From 7fecd0d710d3bd7786df78ee2bc10278ac576008 Mon Sep 17 00:00:00 2001 From: Martin Glagla Date: Tue, 2 Feb 2021 19:25:28 +0100 Subject: [PATCH 2/5] add task::try_current --- src/task/current.rs | 24 ++++++++++++++++++++++-- src/task/mod.rs | 2 +- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/src/task/current.rs b/src/task/current.rs index e4624e15..ad354d62 100644 --- a/src/task/current.rs +++ b/src/task/current.rs @@ -23,6 +23,26 @@ use crate::task::{Task, TaskLocalsWrapper}; /// # }) /// ``` pub fn current() -> Task { - TaskLocalsWrapper::get_current(|t| t.task().clone()) - .expect("`task::current()` called outside the context of a task") + try_current().expect("`task::current()` called outside the context of a task") } + +/// Returns a handle to the current task if called within the context of a task created by [`block_on`], +/// [`spawn`], or [`Builder::spawn`], otherwise returns `None`. +/// +/// [`block_on`]: fn.block_on.html +/// [`spawn`]: fn.spawn.html +/// [`Builder::spawn`]: struct.Builder.html#method.spawn +/// +/// # Examples +/// +/// ``` +/// use async_std::task; +/// +/// match task::try_current() { +/// Some(t) => println!("The name of this task is {:?}", t.name()), +/// None => println!("Not inside a task!"), +/// } +/// ``` +pub fn try_current() -> Option { + TaskLocalsWrapper::get_current(|t| t.task().clone()) +} \ No newline at end of file diff --git a/src/task/mod.rs b/src/task/mod.rs index 440f6ddc..fe574ec6 100644 --- a/src/task/mod.rs +++ b/src/task/mod.rs @@ -133,7 +133,7 @@ cfg_std! { cfg_default! { pub use block_on::block_on; pub use builder::Builder; - pub use current::current; + pub use current::{current, try_current}; pub use task::Task; pub use task_id::TaskId; pub use join_handle::JoinHandle; From 5bc34cb6ba61fd4c748c68c6e251606f14c36659 Mon Sep 17 00:00:00 2001 From: Rolf Karp Date: Sat, 13 Mar 2021 16:22:33 +0100 Subject: [PATCH 3/5] Fix WriteFmtFuture not taking into account already written bytes (#964) --- src/io/write/write_fmt.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/io/write/write_fmt.rs b/src/io/write/write_fmt.rs index d20c41d8..318b1c37 100644 --- a/src/io/write/write_fmt.rs +++ b/src/io/write/write_fmt.rs @@ -11,7 +11,7 @@ pub struct WriteFmtFuture<'a, T: Unpin + ?Sized> { pub(crate) writer: &'a mut T, pub(crate) res: Option>>, pub(crate) buffer: Option>, - pub(crate) amt: u64, + pub(crate) amt: usize, } impl Future for WriteFmtFuture<'_, T> { @@ -37,15 +37,15 @@ impl Future for WriteFmtFuture<'_, T> { // Copy the data from the buffer into the writer until it's done. loop { - if *amt == buffer.len() as u64 { + if *amt == buffer.len() { futures_core::ready!(Pin::new(&mut **writer).poll_flush(cx))?; return Poll::Ready(Ok(())); } - let i = futures_core::ready!(Pin::new(&mut **writer).poll_write(cx, buffer))?; + let i = futures_core::ready!(Pin::new(&mut **writer).poll_write(cx, &buffer[*amt..]))?; if i == 0 { return Poll::Ready(Err(io::ErrorKind::WriteZero.into())); } - *amt += i as u64; + *amt += i; } } } From c4e181cfe1d9c86c25a439cde9433b36aa8fe463 Mon Sep 17 00:00:00 2001 From: Erick Tryzelaar Date: Wed, 31 Mar 2021 11:20:29 -0700 Subject: [PATCH 4/5] Change Incoming impls to only do one allocation This modifies net::tcp::Incoming and os::net::unix::Incoming to only do one allocation, rather than an allocation for each connection. --- src/net/tcp/listener.rs | 27 ++++++--------------------- src/os/unix/net/listener.rs | 24 +++++------------------- 2 files changed, 11 insertions(+), 40 deletions(-) diff --git a/src/net/tcp/listener.rs b/src/net/tcp/listener.rs index cfefc7d2..0825cd92 100644 --- a/src/net/tcp/listener.rs +++ b/src/net/tcp/listener.rs @@ -1,6 +1,6 @@ use std::fmt; -use std::future::Future; use std::net::SocketAddr; +use std::net::TcpStream as StdTcpStream; use std::pin::Pin; use async_io::Async; @@ -148,8 +148,7 @@ impl TcpListener { /// ``` pub fn incoming(&self) -> Incoming<'_> { Incoming { - listener: self, - accept: None, + incoming: Box::pin(self.watcher.incoming()), } } @@ -187,35 +186,21 @@ impl TcpListener { /// [`TcpListener`]: struct.TcpListener.html /// [`std::net::Incoming`]: https://doc.rust-lang.org/std/net/struct.Incoming.html pub struct Incoming<'a> { - listener: &'a TcpListener, - accept: Option< - Pin> + Send + Sync + 'a>>, - >, + incoming: Pin>> + Send + Sync + 'a>>, } impl Stream for Incoming<'_> { type Item = io::Result; fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { - loop { - if self.accept.is_none() { - self.accept = Some(Box::pin(self.listener.accept())); - } - - if let Some(f) = &mut self.accept { - let res = ready!(f.as_mut().poll(cx)); - self.accept = None; - return Poll::Ready(Some(res.map(|(stream, _)| stream))); - } - } + let res = ready!(Pin::new(&mut self.incoming).poll_next(cx)); + Poll::Ready(res.map(|res| res.map(|stream| TcpStream { watcher: Arc::new(stream) }))) } } impl fmt::Debug for Incoming<'_> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - f.debug_struct("Incoming") - .field("listener", self.listener) - .finish() + write!(f, "Incoming {{ ... }}") } } diff --git a/src/os/unix/net/listener.rs b/src/os/unix/net/listener.rs index 3573d7d3..e86502b5 100644 --- a/src/os/unix/net/listener.rs +++ b/src/os/unix/net/listener.rs @@ -1,8 +1,8 @@ //! Unix-specific networking extensions. use std::fmt; -use std::future::Future; use std::os::unix::net::UnixListener as StdUnixListener; +use std::os::unix::net::UnixStream as StdUnixStream; use std::pin::Pin; use async_io::Async; @@ -129,8 +129,7 @@ impl UnixListener { /// ``` pub fn incoming(&self) -> Incoming<'_> { Incoming { - listener: self, - accept: None, + incoming: Box::pin(self.watcher.incoming()), } } @@ -178,34 +177,21 @@ impl fmt::Debug for UnixListener { /// [`incoming`]: struct.UnixListener.html#method.incoming /// [`UnixListener`]: struct.UnixListener.html pub struct Incoming<'a> { - listener: &'a UnixListener, - accept: Option< - Pin> + Send + Sync + 'a>>, - >, + incoming: Pin>> + Send + Sync + 'a>>, } impl Stream for Incoming<'_> { type Item = io::Result; fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { - loop { - if self.accept.is_none() { - self.accept = Some(Box::pin(self.listener.accept())); - } - - if let Some(f) = &mut self.accept { - let res = ready!(f.as_mut().poll(cx)); - self.accept = None; - return Poll::Ready(Some(res.map(|(stream, _)| stream))); - } - } + let res = ready!(Pin::new(&mut self.incoming).poll_next(cx)); + Poll::Ready(res.map(|res| res.map(|stream| UnixStream { watcher: Arc::new(stream) }))) } } impl fmt::Debug for Incoming<'_> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_struct("Incoming") - .field("listener", self.listener) .finish() } } From a410082a7f042ef1325c4cb09c1f5e7e1633ef84 Mon Sep 17 00:00:00 2001 From: Max Davitt Date: Fri, 28 May 2021 12:59:41 -0400 Subject: [PATCH 5/5] Fix typo in Tasks book page --- docs/src/concepts/tasks.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/src/concepts/tasks.md b/docs/src/concepts/tasks.md index 2db8cb0c..c3dbbe20 100644 --- a/docs/src/concepts/tasks.md +++ b/docs/src/concepts/tasks.md @@ -2,7 +2,7 @@ Now that we know what Futures are, we want to run them! -In `async-std`, the [`tasks`][tasks] module is responsible for this. The simplest way is using the `block_on` function: +In `async-std`, the [`task`][tasks] module is responsible for this. The simplest way is using the `block_on` function: ```rust,edition2018 # extern crate async_std;