Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions library/std/src/sync/oneshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,8 @@ pub struct Sender<T> {
inner: mpmc::Sender<T>,
}

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

Expand Down Expand Up @@ -119,6 +118,20 @@ impl<T> Sender<T> {
pub fn send(self, t: T) -> Result<(), SendError<T>> {
self.inner.send(t)
}

/// Returns `true` if the corresponding [`Receiver`] has been dropped.
///
/// If this method returns `true`, [`send`](Self::send) is guaranteed to return an error.
///
/// On the receiving side, [`Receiver::is_ready`] checks whether a value is available without
/// consuming the receiver.
///
/// A return value of `false` does not guarantee that a later call to [`send`](Self::send) will
/// succeed. The receiver may be dropped immediately after this method returns.
#[unstable(feature = "oneshot_channel", issue = "143674")]
pub fn is_closed(&self) -> bool {
self.inner.is_disconnected()
}
}

#[unstable(feature = "oneshot_channel", issue = "143674")]
Expand Down
2 changes: 2 additions & 0 deletions library/std/tests/sync/oneshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use std::{mem, thread};
#[test]
fn is_ready_after_send() {
let (sender, receiver) = oneshot::channel();
assert!(!sender.is_closed());
assert_eq!(receiver.is_ready(), Ok(false));

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

mem::drop(receiver);

assert!(sender.is_closed());
let send_error = sender.send(32u128).unwrap_err();
assert_eq!(send_error.0, 32);
}
Expand Down
Loading