Skip to content

Commit d9f802f

Browse files
authored
Rollup merge of #163883 - connortsui20:oneshot-is-closed, r=clarfonthey
Add `oneshot` `Sender::is_closed` Tracking Issue: #143674 Based on top of #152557, so I'll make this a reviewable PR once that merges. Adds `oneshot` `Sender::is_closed` method (inspired by #152557 (comment)). The tracking issue has been updated.
2 parents 60048ab + 2d21f3a commit d9f802f

2 files changed

Lines changed: 18 additions & 3 deletions

File tree

‎library/std/src/sync/oneshot.rs‎

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,8 @@ pub struct Sender<T> {
8686
inner: mpmc::Sender<T>,
8787
}
8888

89-
// SAFETY: Since the only methods in which synchronization must occur take full ownership of the
90-
// [`Sender`], it is perfectly safe to share a `&Sender` between threads (as it is effectively
91-
// useless without ownership).
89+
// SAFETY: Sending a value requires ownership of the sender. Shared references only inspect
90+
// channel state, so they cannot send a value of a non-`Send` type from another thread.
9291
#[unstable(feature = "oneshot_channel", issue = "143674")]
9392
unsafe impl<T> Sync for Sender<T> {}
9493

@@ -119,6 +118,20 @@ impl<T> Sender<T> {
119118
pub fn send(self, t: T) -> Result<(), SendError<T>> {
120119
self.inner.send(t)
121120
}
121+
122+
/// Returns `true` if the corresponding [`Receiver`] has been dropped.
123+
///
124+
/// If this method returns `true`, [`send`](Self::send) is guaranteed to return an error.
125+
///
126+
/// On the receiving side, [`Receiver::is_ready`] checks whether a value is available without
127+
/// consuming the receiver.
128+
///
129+
/// A return value of `false` does not guarantee that a later call to [`send`](Self::send) will
130+
/// succeed. The receiver may be dropped immediately after this method returns.
131+
#[unstable(feature = "oneshot_channel", issue = "143674")]
132+
pub fn is_closed(&self) -> bool {
133+
self.inner.is_disconnected()
134+
}
122135
}
123136

124137
#[unstable(feature = "oneshot_channel", issue = "143674")]

‎library/std/tests/sync/oneshot.rs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use std::{mem, thread};
99
#[test]
1010
fn is_ready_after_send() {
1111
let (sender, receiver) = oneshot::channel();
12+
assert!(!sender.is_closed());
1213
assert_eq!(receiver.is_ready(), Ok(false));
1314

1415
sender.send(String::from("hello")).unwrap();
@@ -126,6 +127,7 @@ fn send_error() {
126127

127128
mem::drop(receiver);
128129

130+
assert!(sender.is_closed());
129131
let send_error = sender.send(32u128).unwrap_err();
130132
assert_eq!(send_error.0, 32);
131133
}

0 commit comments

Comments
 (0)