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

sync: add notify method to Receiver from watch module #2490

Closed
wants to merge 2 commits into from

Conversation

govardhangdg
Copy link

@govardhangdg govardhangdg commented May 5, 2020

Motivation

Attempt to solve #2404

Solution

Modified recv method to return Option<()> instead of Option< T >
I have added documentation test.

tokio/src/sync/watch.rs Outdated Show resolved Hide resolved
Copy link
Contributor

@Darksonn Darksonn left a comment

Choose a reason for hiding this comment

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

We should consider if Option<()> is the best return value here. Additionally, as I mentioned in #2404, I'm not convinced that notify is the best name for the function.

@Darksonn Darksonn added A-tokio Area: The main tokio crate C-enhancement Category: A PR with an enhancement or bugfix. M-sync Module: tokio/sync labels May 5, 2020
Co-authored-by: Alice Ryhl <alice@ryhl.io>
@Darksonn
Copy link
Contributor

Darksonn commented May 6, 2020

Huh, apparently it doesn't compile if you move the map outside due to the lifetime on the reference. I guess you should move it inside again.

I've thought a bit more about the Option<()> thing, and realized that we can totally do this:

pub async fn recv_ref<'a>(&'a mut self) -> Option<Ref<'a, T>> {
    let has_next = poll_fn(|cx| self.poll_recv_ref(cx).map(|opt| opt.is_some())).await;
    if has_next {
        Some(self.borrow())
    } else {
        None
    }
}

This seems like a better approach than simply waiting.

/// assert!(v.is_none());
/// }
/// ```
pub async fn notify(&mut self) -> Option<()> {
Copy link
Member

Choose a reason for hiding this comment

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

We probably want to name this notified to match closed.

Then, it would be used as: rx.notified().await.

@Darksonn
Copy link
Contributor

Are you interested in getting this PR across the finish line?

@carllerche
Copy link
Member

This has been done in #2814. Thanks for the proposal and getting the work started 👍

@carllerche carllerche closed this Sep 23, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-tokio Area: The main tokio crate C-enhancement Category: A PR with an enhancement or bugfix. M-sync Module: tokio/sync
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants