@@ -989,6 +989,91 @@ impl<T> Receiver<T> {
989
989
self . inner . is_none ( )
990
990
}
991
991
992
+ /// Checks if a channel is empty.
993
+ ///
994
+ /// This method returns `true` if the channel has no messages.
995
+ ///
996
+ /// It is not necessarily safe to poll an empty receiver, which may have
997
+ /// already yielded a value. Use [`is_terminated()`][Self::is_terminated]
998
+ /// to check whether or not a receiver can be safely polled, instead.
999
+ ///
1000
+ /// # Examples
1001
+ ///
1002
+ /// Sending a value.
1003
+ ///
1004
+ /// ```
1005
+ /// use tokio::sync::oneshot;
1006
+ ///
1007
+ /// #[tokio::main]
1008
+ /// async fn main() {
1009
+ /// let (tx, mut rx) = oneshot::channel();
1010
+ /// assert!(rx.is_empty());
1011
+ ///
1012
+ /// tx.send(0).unwrap();
1013
+ /// assert!(!rx.is_empty());
1014
+ ///
1015
+ /// let _ = (&mut rx).await;
1016
+ /// assert!(rx.is_empty());
1017
+ /// }
1018
+ /// ```
1019
+ ///
1020
+ /// Dropping the sender.
1021
+ ///
1022
+ /// ```
1023
+ /// use tokio::sync::oneshot;
1024
+ ///
1025
+ /// #[tokio::main]
1026
+ /// async fn main() {
1027
+ /// let (tx, mut rx) = oneshot::channel::<()>();
1028
+ ///
1029
+ /// // A channel is empty if the sender is dropped.
1030
+ /// drop(tx);
1031
+ /// assert!(rx.is_empty());
1032
+ ///
1033
+ /// // A closed channel still yields an error, however.
1034
+ /// (&mut rx).await.expect_err("should yield an error");
1035
+ /// assert!(rx.is_empty());
1036
+ /// }
1037
+ /// ```
1038
+ ///
1039
+ /// Terminated channels are empty.
1040
+ ///
1041
+ /// ```should_panic
1042
+ /// use tokio::sync::oneshot;
1043
+ ///
1044
+ /// #[tokio::main]
1045
+ /// async fn main() {
1046
+ /// let (tx, mut rx) = oneshot::channel();
1047
+ /// tx.send(0).unwrap();
1048
+ /// let _ = (&mut rx).await;
1049
+ ///
1050
+ /// // NB: an empty channel is not necessarily safe to poll!
1051
+ /// assert!(rx.is_empty());
1052
+ /// let _ = (&mut rx).await;
1053
+ /// }
1054
+ /// ```
1055
+ pub fn is_empty ( & self ) -> bool {
1056
+ let Some ( inner) = self . inner . as_ref ( ) else {
1057
+ // The channel has already terminated.
1058
+ return true ;
1059
+ } ;
1060
+
1061
+ let state = State :: load ( & inner. state , Acquire ) ;
1062
+ if state. is_complete ( ) {
1063
+ // SAFETY: If `state.is_complete()` returns true, then the
1064
+ // `VALUE_SENT` bit has been set and the sender side of the
1065
+ // channel will no longer attempt to access the inner
1066
+ // `UnsafeCell`. Therefore, it is now safe for us to access the
1067
+ // cell.
1068
+ //
1069
+ // The channel is empty if it does not have a value.
1070
+ unsafe { !inner. has_value ( ) }
1071
+ } else {
1072
+ // The receiver closed the channel or no value has been sent yet.
1073
+ true
1074
+ }
1075
+ }
1076
+
992
1077
/// Attempts to receive a value.
993
1078
///
994
1079
/// If a pending value exists in the channel, it is returned. If no value
@@ -1291,6 +1376,19 @@ impl<T> Inner<T> {
1291
1376
unsafe fn consume_value ( & self ) -> Option < T > {
1292
1377
self . value . with_mut ( |ptr| ( * ptr) . take ( ) )
1293
1378
}
1379
+
1380
+ /// Returns true if there is a value. This function does not check `state`.
1381
+ ///
1382
+ /// # Safety
1383
+ ///
1384
+ /// Calling this method concurrently on multiple threads will result in a
1385
+ /// data race. The `VALUE_SENT` state bit is used to ensure that only the
1386
+ /// sender *or* the receiver will call this method at a given point in time.
1387
+ /// If `VALUE_SENT` is not set, then only the sender may call this method;
1388
+ /// if it is set, then only the receiver may call this method.
1389
+ unsafe fn has_value ( & self ) -> bool {
1390
+ self . value . with ( |ptr| ( * ptr) . is_some ( ) )
1391
+ }
1294
1392
}
1295
1393
1296
1394
unsafe impl < T : Send > Send for Inner < T > { }
0 commit comments