sync: add async APIs to oneshot and mpsc (#1211)

Adds:

- oneshot::Sender::close
- mpsc::Receiver::recv
- mpsc::Sender::send

Also renames `poll_next` to `poll_recv`.

Refs: #1210
This commit is contained in:
Carl Lerche
2019-06-27 11:33:36 -07:00
committed by GitHub
parent 0af05e7408
commit 32ceccb465
10 changed files with 143 additions and 41 deletions
+1
View File
@@ -7,6 +7,7 @@
)]
#![cfg_attr(test, deny(warnings))]
#![doc(test(no_crate_inject, attr(deny(rust_2018_idioms))))]
#![feature(async_await)]
//! Asynchronous synchronization primitives.
//!
+24 -2
View File
@@ -132,7 +132,14 @@ impl<T> Receiver<T> {
}
/// TODO: Dox
pub fn poll_next(&mut self, cx: &mut Context<'_>) -> Poll<Option<T>> {
pub async fn recv(&mut self) -> Option<T> {
use async_util::future::poll_fn;
poll_fn(|cx| self.poll_recv(cx)).await
}
/// TODO: Dox
pub fn poll_recv(&mut self, cx: &mut Context<'_>) -> Poll<Option<T>> {
self.chan.recv(cx)
}
@@ -150,7 +157,7 @@ impl<T> futures_core::Stream for Receiver<T> {
type Item = T;
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<T>> {
Receiver::poll_next(self.get_mut(), cx)
self.get_mut().poll_recv(cx)
}
}
@@ -189,6 +196,21 @@ impl<T> Sender<T> {
self.chan.try_send(message)?;
Ok(())
}
/// Send a value, waiting until there is capacity.
///
/// # Examples
///
/// ```
/// unimplemented!();
/// ```
pub async fn send(&mut self, value: T) -> Result<(), SendError> {
use async_util::future::poll_fn;
poll_fn(|cx| self.poll_ready(cx)).await?;
self.try_send(value).map_err(|_| SendError(()))
}
}
#[cfg(feature = "async-traits")]
+8 -1
View File
@@ -88,10 +88,17 @@ impl<T> UnboundedReceiver<T> {
}
/// TODO: dox
pub fn poll_next(&mut self, cx: &mut Context<'_>) -> Poll<Option<T>> {
pub fn poll_recv(&mut self, cx: &mut Context<'_>) -> Poll<Option<T>> {
self.chan.recv(cx)
}
/// TODO: Dox
pub async fn recv(&mut self) -> Option<T> {
use async_util::future::poll_fn;
poll_fn(|cx| self.poll_recv(cx)).await
}
/// Closes the receiving half of a channel, without dropping it.
///
/// This prevents any further messages from being sent on the channel while
+19
View File
@@ -217,6 +217,25 @@ impl<T> Sender<T> {
Pending
}
/// Wait for the associated [`Receiver`] handle to drop.
///
/// # Return
///
/// Returns a `Future` which must be awaited on.
///
/// [`Receiver`]: struct.Receiver.html
///
/// # Examples
///
/// ```
/// unimplemented!();
/// ```
pub async fn closed(&mut self) {
use async_util::future::poll_fn;
poll_fn(|cx| self.poll_close(cx)).await
}
/// Check if the associated [`Receiver`] handle has been dropped.
///
/// Unlike [`poll_close`], this function does not register a task for