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

signal: make Signal::poll_recv method public #3383

Merged
merged 1 commit into from
Feb 5, 2021
Merged
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
36 changes: 35 additions & 1 deletion tokio/src/signal/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,41 @@ impl Signal {
poll_fn(|cx| self.poll_recv(cx)).await
}

pub(crate) fn poll_recv(&mut self, cx: &mut Context<'_>) -> Poll<Option<()>> {
/// Polls to receive the next signal notification event, outside of an
/// `async` context.
///
/// This method returns:
///
/// * `Poll::Pending` if no signals are available but the channel is not
/// closed.
/// * `Poll::Ready(Some(()))` if a signal is available.
/// * `Poll::Ready(None)` if the channel has been closed and all signals
/// sent before it was closed have been received.
///
/// # Examples
///
/// Polling from a manually implemented future
///
/// ```rust,no_run
/// use std::pin::Pin;
/// use std::future::Future;
/// use std::task::{Context, Poll};
/// use tokio::signal::unix::Signal;
///
/// struct MyFuture {
/// signal: Signal,
/// }
///
/// impl Future for MyFuture {
/// type Output = Option<()>;
///
/// fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
/// println!("polling MyFuture");
/// self.signal.poll_recv(cx)
/// }
/// }
/// ```
pub fn poll_recv(&mut self, cx: &mut Context<'_>) -> Poll<Option<()>> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Darksonn mentioned it, but we probably should come up w/ a better return type here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree its a bit awkward to return Option<()> when we don't effectively return None, but should we keep parity with async fn recv() -> Option<()>?

The original intention was to use the Stream API directly and not have either of these APIs, but that didn't happen...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Modeling after std, if the fn is public, the return type should be Result where the error signals "terminated".

Modeled after: https://doc.rust-lang.org/stable/std/sync/mpsc/struct.Receiver.html

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't even follow that on our channels, I think.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like we have not followed the Result pattern for other poll_recv fns, so lets just keep it as is.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since it is already exposed on the windows variants with this return type, and for consistency with the non-poll variant, we are keeping this return type.

self.rx.poll_recv(cx)
}

Expand Down