Skip to content

Commit 3db22e2

Browse files
authored
sync: documentation for mpsc channels (#2600)
1 parent e2adf26 commit 3db22e2

File tree

3 files changed

+42
-6
lines changed

3 files changed

+42
-6
lines changed

tokio/src/sync/mpsc/mod.rs

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,13 @@
1010
//! is rejected and the task will be notified when additional capacity is
1111
//! available. In other words, the channel provides backpressure.
1212
//!
13-
//! Unbounded channels are also available using the `unbounded_channel`
14-
//! constructor.
13+
//! This module provides two variants of the channel: A bounded and an unbounded
14+
//! variant. The bounded variant has a limit on the number of messages that the
15+
//! channel can store, and if this limit is reached, trying to send another
16+
//! message will sleep until a message is received from the channel. An unbounded
17+
//! channel has an infinite capacity, so the `send` method never does any kind of
18+
//! sleeping. This makes the [`UnboundedSender`] usable from both synchronous and
19+
//! asynchronous code.
1520
//!
1621
//! # Disconnection
1722
//!
@@ -32,8 +37,32 @@
3237
//! consumes the channel to completion, at which point the receiver can be
3338
//! dropped.
3439
//!
40+
//! # Communicating between sync and async code
41+
//!
42+
//! When you want to communicate between synchronous and asynchronous code, there
43+
//! are two situations to consider:
44+
//!
45+
//! **Bounded channel**: If you need a bounded channel, you should use a bounded
46+
//! Tokio `mpsc` channel for both directions of communication. To call the async
47+
//! [`send`][bounded-send] or [`recv`][bounded-recv] methods in sync code, you
48+
//! will need to use [`Handle::block_on`], which allow you to execute an async
49+
//! method in synchronous code. This is necessary because a bounded channel may
50+
//! need to wait for additional capacity to become available.
51+
//!
52+
//! **Unbounded channel**: You should use the kind of channel that matches where
53+
//! the receiver is. So for sending a message _from async to sync_, you should
54+
//! use [the standard library unbounded channel][std-unbounded] or
55+
//! [crossbeam][crossbeam-unbounded]. Similarly, for sending a message _from sync
56+
//! to async_, you should use an unbounded Tokio `mpsc` channel.
57+
//!
3558
//! [`Sender`]: crate::sync::mpsc::Sender
3659
//! [`Receiver`]: crate::sync::mpsc::Receiver
60+
//! [bounded-send]: crate::sync::mpsc::Sender::send()
61+
//! [bounded-recv]: crate::sync::mpsc::Receiver::recv()
62+
//! [`UnboundedSender`]: crate::sync::mpsc::UnboundedSender
63+
//! [`Handle::block_on`]: crate::runtime::Handle::block_on()
64+
//! [std-unbounded]: std::sync::mpsc::channel
65+
//! [crossbeam-unbounded]: https://docs.rs/crossbeam/*/crossbeam/channel/fn.unbounded.html
3766
3867
pub(super) mod block;
3968

tokio/src/sync/mpsc/unbounded.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,9 +163,13 @@ impl<T> UnboundedSender<T> {
163163

164164
/// Attempts to send a message on this `UnboundedSender` without blocking.
165165
///
166+
/// This method is not marked async because sending a message to an unbounded channel
167+
/// never requires any form of waiting. Because of this, the `send` method can be
168+
/// used in both synchronous and asynchronous code without problems.
169+
///
166170
/// If the receive half of the channel is closed, either due to [`close`]
167-
/// being called or the [`UnboundedReceiver`] having been dropped,
168-
/// the function returns an error. The error includes the value passed to `send`.
171+
/// being called or the [`UnboundedReceiver`] having been dropped, this
172+
/// function returns an error. The error includes the value passed to `send`.
169173
///
170174
/// [`close`]: UnboundedReceiver::close
171175
/// [`UnboundedReceiver`]: UnboundedReceiver

tokio/src/sync/oneshot.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,11 @@ impl<T> Sender<T> {
144144
/// Attempts to send a value on this channel, returning it back if it could
145145
/// not be sent.
146146
///
147-
/// The function consumes `self` as only one value may ever be sent on a
148-
/// one-shot channel.
147+
/// This method consumes `self` as only one value may ever be sent on a oneshot
148+
/// channel. It is not marked async because sending a message to an oneshot
149+
/// channel never requires any form of waiting. Because of this, the `send`
150+
/// method can be used in both synchronous and asynchronous code without
151+
/// problems.
149152
///
150153
/// A successful send occurs when it is determined that the other end of the
151154
/// channel has not hung up already. An unsuccessful send would be one where

0 commit comments

Comments
 (0)