@@ -330,6 +330,73 @@ impl<T> UnboundedReceiver<T> {
330
330
self . chan . close ( ) ;
331
331
}
332
332
333
+ /// Checks if a channel is closed.
334
+ ///
335
+ /// This method returns `true` if the channel has been closed. The channel is closed
336
+ /// when all [`UnboundedSender`] have been dropped, or when [`UnboundedReceiver::close`] is called.
337
+ ///
338
+ /// [`UnboundedSender`]: crate::sync::mpsc::UnboundedSender
339
+ /// [`UnboundedReceiver::close`]: crate::sync::mpsc::UnboundedReceiver::close
340
+ ///
341
+ /// # Examples
342
+ /// ```
343
+ /// use tokio::sync::mpsc;
344
+ ///
345
+ /// #[tokio::main]
346
+ /// async fn main() {
347
+ /// let (_tx, mut rx) = mpsc::unbounded_channel::<()>();
348
+ /// assert!(!rx.is_closed());
349
+ ///
350
+ /// rx.close();
351
+ ///
352
+ /// assert!(rx.is_closed());
353
+ /// }
354
+ /// ```
355
+ pub fn is_closed ( & self ) -> bool {
356
+ self . chan . is_closed ( )
357
+ }
358
+
359
+ /// Checks if a channel is empty.
360
+ ///
361
+ /// This method returns `true` if the channel has no messages.
362
+ ///
363
+ /// # Examples
364
+ /// ```
365
+ /// use tokio::sync::mpsc;
366
+ ///
367
+ /// #[tokio::main]
368
+ /// async fn main() {
369
+ /// let (tx, rx) = mpsc::unbounded_channel();
370
+ /// assert!(rx.is_empty());
371
+ ///
372
+ /// tx.send(0).unwrap();
373
+ /// assert!(!rx.is_empty());
374
+ /// }
375
+ ///
376
+ /// ```
377
+ pub fn is_empty ( & self ) -> bool {
378
+ self . chan . is_empty ( )
379
+ }
380
+
381
+ /// Returns the number of messages in the channel.
382
+ ///
383
+ /// # Examples
384
+ /// ```
385
+ /// use tokio::sync::mpsc;
386
+ ///
387
+ /// #[tokio::main]
388
+ /// async fn main() {
389
+ /// let (tx, rx) = mpsc::unbounded_channel();
390
+ /// assert_eq!(0, rx.len());
391
+ ///
392
+ /// tx.send(0).unwrap();
393
+ /// assert_eq!(1, rx.len());
394
+ /// }
395
+ /// ```
396
+ pub fn len ( & self ) -> usize {
397
+ self . chan . len ( )
398
+ }
399
+
333
400
/// Polls to receive the next message on this channel.
334
401
///
335
402
/// This method returns:
0 commit comments