@@ -343,8 +343,8 @@ impl<T> UnboundedReceiver<T> {
343
343
/// When the method returns `Poll::Pending`, the `Waker` in the provided
344
344
/// `Context` is scheduled to receive a wakeup when a message is sent on any
345
345
/// receiver, or when the channel is closed. Note that on multiple calls to
346
- /// `poll_recv`, only the `Waker` from the `Context` passed to the most
347
- /// recent call is scheduled to receive a wakeup.
346
+ /// `poll_recv` or `poll_recv_many` , only the `Waker` from the `Context`
347
+ /// passed to the most recent call is scheduled to receive a wakeup.
348
348
///
349
349
/// If this method returns `Poll::Pending` due to a spurious failure, then
350
350
/// the `Waker` will be notified when the situation causing the spurious
@@ -354,6 +354,83 @@ impl<T> UnboundedReceiver<T> {
354
354
pub fn poll_recv ( & mut self , cx : & mut Context < ' _ > ) -> Poll < Option < T > > {
355
355
self . chan . recv ( cx)
356
356
}
357
+
358
+ /// Polls to receive multiple messages on this channel, extending the provided buffer.
359
+ ///
360
+ /// This method returns:
361
+ /// * `Poll::Pending` if no messages are available but the channel is not closed, or if a
362
+ /// spurious failure happens.
363
+ /// * `Poll::Ready(count)` where `count` is the number of messages successfully received and
364
+ /// stored in `buffer`. This can be less than, or equal to, `limit`.
365
+ /// * `Poll::Ready(0)` if `limit` is set to zero or when the channel is closed.
366
+ ///
367
+ /// When the method returns `Poll::Pending`, the `Waker` in the provided
368
+ /// `Context` is scheduled to receive a wakeup when a message is sent on any
369
+ /// receiver, or when the channel is closed. Note that on multiple calls to
370
+ /// `poll_recv` or `poll_recv_many`, only the `Waker` from the `Context`
371
+ /// passed to the most recent call is scheduled to receive a wakeup.
372
+ ///
373
+ /// Note that this method does not guarantee that exactly `limit` messages
374
+ /// are received. Rather, if at least one message is available, it returns
375
+ /// as many messages as it can up to the given limit. This method returns
376
+ /// zero only if the channel is closed (or if `limit` is zero).
377
+ ///
378
+ /// # Examples
379
+ ///
380
+ /// ```
381
+ /// use std::task::{Context, Poll};
382
+ /// use std::pin::Pin;
383
+ /// use tokio::sync::mpsc;
384
+ /// use futures::Future;
385
+ ///
386
+ /// struct MyReceiverFuture<'a> {
387
+ /// receiver: mpsc::UnboundedReceiver<i32>,
388
+ /// buffer: &'a mut Vec<i32>,
389
+ /// limit: usize,
390
+ /// }
391
+ ///
392
+ /// impl<'a> Future for MyReceiverFuture<'a> {
393
+ /// type Output = usize; // Number of messages received
394
+ ///
395
+ /// fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
396
+ /// let MyReceiverFuture { receiver, buffer, limit } = &mut *self;
397
+ ///
398
+ /// // Now `receiver` and `buffer` are mutable references, and `limit` is copied
399
+ /// match receiver.poll_recv_many(cx, *buffer, *limit) {
400
+ /// Poll::Pending => Poll::Pending,
401
+ /// Poll::Ready(count) => Poll::Ready(count),
402
+ /// }
403
+ /// }
404
+ /// }
405
+ ///
406
+ /// #[tokio::main]
407
+ /// async fn main() {
408
+ /// let (tx, rx) = mpsc::unbounded_channel::<i32>();
409
+ /// let mut buffer = Vec::new();
410
+ ///
411
+ /// let my_receiver_future = MyReceiverFuture {
412
+ /// receiver: rx,
413
+ /// buffer: &mut buffer,
414
+ /// limit: 3,
415
+ /// };
416
+ ///
417
+ /// for i in 0..10 {
418
+ /// tx.send(i).expect("Unable to send integer");
419
+ /// }
420
+ ///
421
+ /// let count = my_receiver_future.await;
422
+ /// assert_eq!(count, 3);
423
+ /// assert_eq!(buffer, vec![0,1,2])
424
+ /// }
425
+ /// ```
426
+ pub fn poll_recv_many (
427
+ & mut self ,
428
+ cx : & mut Context < ' _ > ,
429
+ buffer : & mut Vec < T > ,
430
+ limit : usize ,
431
+ ) -> Poll < usize > {
432
+ self . chan . recv_many ( cx, buffer, limit)
433
+ }
357
434
}
358
435
359
436
impl < T > UnboundedSender < T > {
0 commit comments