|
10 | 10 | //! is rejected and the task will be notified when additional capacity is
|
11 | 11 | //! available. In other words, the channel provides backpressure.
|
12 | 12 | //!
|
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. |
15 | 20 | //!
|
16 | 21 | //! # Disconnection
|
17 | 22 | //!
|
|
32 | 37 | //! consumes the channel to completion, at which point the receiver can be
|
33 | 38 | //! dropped.
|
34 | 39 | //!
|
| 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 | +//! |
35 | 58 | //! [`Sender`]: crate::sync::mpsc::Sender
|
36 | 59 | //! [`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 |
37 | 66 |
|
38 | 67 | pub(super) mod block;
|
39 | 68 |
|
|
0 commit comments