Skip to content

Commit 4d665e6

Browse files
committed
sync: improve the docs for recv_many
Trying to explain a bit better when `recv_many` will return and why in some cases the returned number of received messages is below the input parameter `limit`. Additionally the docs for the bounded and unbounded `recv_many` are adjusted to match.
1 parent e076d21 commit 4d665e6

File tree

2 files changed

+25
-12
lines changed

2 files changed

+25
-12
lines changed

tokio/src/sync/mpsc/bounded.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -248,12 +248,17 @@ impl<T> Receiver<T> {
248248
/// as specified by `limit`. If `limit` is zero, the function immediately
249249
/// returns `0`. The return value is the number of values added to `buffer`.
250250
///
251-
/// For `limit > 0`, if there are no messages in the channel's queue, but
252-
/// the channel has not yet been closed, this method will sleep until a
253-
/// message is sent or the channel is closed. Note that if [`close`] is
254-
/// called, but there are still outstanding [`Permits`] from before it was
255-
/// closed, the channel is not considered closed by `recv_many` until the
256-
/// permits are released.
251+
/// For `limit > 0`: If there are no messages in the channels' queue initially,
252+
/// `recv_many` will sleep until a first message is received. After that, it will
253+
///
254+
/// * Return when the number of received messages reaches `limit`.
255+
/// * Return earlier when the channel is closed or no further messages area
256+
/// available in the channel at the time. In these cases,
257+
/// the number of received messages can be lower than `limit`.
258+
///
259+
/// Note that if [`close`] is called, but there are still outstanding [`Permits`]
260+
/// from before it was closed, the channel is not considered closed by
261+
/// `recv_many` until the permits are released.
257262
///
258263
/// For non-zero values of `limit`, this method will never return `0` unless
259264
/// the channel has been closed and there are no remaining messages in the

tokio/src/sync/mpsc/unbounded.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -173,13 +173,20 @@ impl<T> UnboundedReceiver<T> {
173173
/// Receives the next values for this receiver and extends `buffer`.
174174
///
175175
/// This method extends `buffer` by no more than a fixed number of values
176-
/// as specified by `limit`. If `limit` is zero, the function returns
177-
/// immediately with `0`. The return value is the number of values added to
178-
/// `buffer`.
176+
/// as specified by `limit`. If `limit` is zero, the function immediately
177+
/// returns `0`. The return value is the number of values added to `buffer`.
179178
///
180-
/// For `limit > 0`, if there are no messages in the channel's queue,
181-
/// but the channel has not yet been closed, this method will sleep
182-
/// until a message is sent or the channel is closed.
179+
/// For `limit > 0`: If there are no messages in the channels' queue initially,
180+
/// `recv_many` will sleep until a first message is received. After that, it will
181+
///
182+
/// * Return when the number of received messages reaches `limit`.
183+
/// * Return earlier when the channel is closed or no further messages area
184+
/// available in the channel at the time. In these cases,
185+
/// the number of received messages can be lower than `limit`.
186+
///
187+
/// Note that if [`close`] is called, but there are still outstanding [`Permits`]
188+
/// from before it was closed, the channel is not considered closed by
189+
/// `recv_many` until the permits are released.
183190
///
184191
/// For non-zero values of `limit`, this method will never return `0` unless
185192
/// the channel has been closed and there are no remaining messages in the
@@ -197,6 +204,7 @@ impl<T> UnboundedReceiver<T> {
197204
/// channel.
198205
///
199206
/// [`close`]: Self::close
207+
/// [`Permits`]: struct@crate::sync::mpsc::Permit
200208
///
201209
/// # Examples
202210
///

0 commit comments

Comments
 (0)