@@ -931,6 +931,64 @@ impl<T> Receiver<T> {
931
931
}
932
932
}
933
933
934
+ /// Checks if this receiver is terminated.
935
+ ///
936
+ /// This function returns true if this receiver has already yielded a [`Poll::Ready`] result.
937
+ /// If so, this receiver should no longer be polled.
938
+ ///
939
+ /// # Examples
940
+ ///
941
+ /// Sending a value and polling it.
942
+ ///
943
+ /// ```
944
+ /// use tokio::sync::oneshot;
945
+ ///
946
+ /// use std::task::Poll;
947
+ ///
948
+ /// #[tokio::main]
949
+ /// async fn main() {
950
+ /// let (tx, mut rx) = oneshot::channel();
951
+ ///
952
+ /// // A receiver is not terminated when it is initialized.
953
+ /// assert!(!rx.is_terminated());
954
+ ///
955
+ /// // A receiver is not terminated it is polled and is still pending.
956
+ /// let poll = futures::poll!(&mut rx);
957
+ /// assert_eq!(poll, Poll::Pending);
958
+ /// assert!(!rx.is_terminated());
959
+ ///
960
+ /// // A receiver is not terminated if a value has been sent, but not yet read.
961
+ /// tx.send(0).unwrap();
962
+ /// assert!(!rx.is_terminated());
963
+ ///
964
+ /// // A receiver *is* terminated after it has been polled and yielded a value.
965
+ /// assert_eq!((&mut rx).await, Ok(0));
966
+ /// assert!(rx.is_terminated());
967
+ /// }
968
+ /// ```
969
+ ///
970
+ /// Dropping the sender.
971
+ ///
972
+ /// ```
973
+ /// use tokio::sync::oneshot;
974
+ ///
975
+ /// #[tokio::main]
976
+ /// async fn main() {
977
+ /// let (tx, mut rx) = oneshot::channel::<()>();
978
+ ///
979
+ /// // A receiver is not immediately terminated when the sender is dropped.
980
+ /// drop(tx);
981
+ /// assert!(!rx.is_terminated());
982
+ ///
983
+ /// // A receiver *is* terminated after it has been polled and yielded an error.
984
+ /// let _ = (&mut rx).await.unwrap_err();
985
+ /// assert!(rx.is_terminated());
986
+ /// }
987
+ /// ```
988
+ pub fn is_terminated ( & self ) -> bool {
989
+ self . inner . is_none ( )
990
+ }
991
+
934
992
/// Attempts to receive a value.
935
993
///
936
994
/// If a pending value exists in the channel, it is returned. If no value
@@ -1106,18 +1164,18 @@ impl<T> Future for Receiver<T> {
1106
1164
1107
1165
let ret = if let Some ( inner) = self . as_ref ( ) . get_ref ( ) . inner . as_ref ( ) {
1108
1166
#[ cfg( all( tokio_unstable, feature = "tracing" ) ) ]
1109
- let res = ready ! ( trace_poll_op!( "poll_recv" , inner. poll_recv( cx) ) ) ? ;
1167
+ let res = ready ! ( trace_poll_op!( "poll_recv" , inner. poll_recv( cx) ) ) . map_err ( Into :: into ) ;
1110
1168
1111
1169
#[ cfg( any( not( tokio_unstable) , not( feature = "tracing" ) ) ) ]
1112
- let res = ready ! ( inner. poll_recv( cx) ) ? ;
1170
+ let res = ready ! ( inner. poll_recv( cx) ) . map_err ( Into :: into ) ;
1113
1171
1114
1172
res
1115
1173
} else {
1116
1174
panic ! ( "called after complete" ) ;
1117
1175
} ;
1118
1176
1119
1177
self . inner = None ;
1120
- Ready ( Ok ( ret) )
1178
+ Ready ( ret)
1121
1179
}
1122
1180
}
1123
1181
0 commit comments