mirror of
https://github.com/tokio-rs/tokio.git
synced 2026-08-28 00:00:11 +02:00
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:
@@ -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")]
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user