Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow oneshot::Receiver::close after successful try_recv #3552

Merged
merged 1 commit into from Feb 25, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 6 additions & 2 deletions tokio/src/sync/oneshot.rs
Expand Up @@ -443,6 +443,9 @@ impl<T> Receiver<T> {
/// This function is useful to perform a graceful shutdown and ensure that a
/// value will not be sent into the channel and never received.
///
/// `close` is no-op if a message is already received or the channel
/// is already closed.
///
/// [`Sender`]: Sender
/// [`try_recv`]: Receiver::try_recv
///
Expand Down Expand Up @@ -490,8 +493,9 @@ impl<T> Receiver<T> {
/// }
/// ```
pub fn close(&mut self) {
let inner = self.inner.as_ref().unwrap();
inner.close();
if let Some(inner) = self.inner.as_ref() {
inner.close();
}
}

/// Attempts to receive a value.
Expand Down
10 changes: 10 additions & 0 deletions tokio/tests/sync_oneshot.rs
Expand Up @@ -180,6 +180,16 @@ fn close_try_recv_poll() {
let _ = rx.poll();
}

#[test]
fn close_after_recv() {
let (tx, mut rx) = oneshot::channel::<i32>();

tx.send(17).unwrap();

assert_eq!(17, rx.try_recv().unwrap());
rx.close();
}

#[test]
fn drops_tasks() {
let (mut tx, mut rx) = oneshot::channel::<i32>();
Expand Down