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 same_channel method to watch::Receiver #4579

Closed
ahrens opened this issue Mar 23, 2022 · 1 comment · Fixed by #4581
Closed

sync: add same_channel method to watch::Receiver #4579

ahrens opened this issue Mar 23, 2022 · 1 comment · Fixed by #4581
Labels
A-tokio Area: The main tokio crate C-feature-request Category: A feature request. M-sync Module: tokio/sync

Comments

@ahrens
Copy link
Contributor

ahrens commented Mar 23, 2022

Is your feature request related to a problem? Please describe.
I'd like to determine if two watch::Receivers are part of the same channel (i.e. will receive values from the same watch::Sender.

Specifically, I'd like to have a HashMap<K, watch::Receiver<V>>, and then later be able to check if my entry is still there, or someone else has removed it (and potentially replaced it with a Receiver for a different Sender).

Describe the solution you'd like
Add a method similar to:

impl<T> Receiver<T> {
    pub fn same_channel(&self, other: &Self) -> bool {
        Arc::ptr_eq(&self.shared, &other.shared)
    }
} 

Describe alternatives you've considered
The solution I have today is to use a HashMap<K, Arc<watch::Sender<V>> and then use Arc::ptr_eq() to check if the Sender is mine. The disadvantage is that there's an additional heap allocation/indirection for the Arc, and I need to ensure that no extraneous clones of the Arc are created.

Additional context
I noticed that @zh-jq opened #3532 to add mpsc::Sender::same_channel() and @Darksonn reviewed it. This is similar functionality.

I'd be willing to try to implement/upstream this, if it's likely to be accepted.

@ahrens ahrens added A-tokio Area: The main tokio crate C-feature-request Category: A feature request. labels Mar 23, 2022
@Darksonn Darksonn added the M-sync Module: tokio/sync label Mar 23, 2022
@Darksonn
Copy link
Contributor

Adding this seems fine to me.

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-feature-request Category: A feature request. M-sync Module: tokio/sync
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants