tokio-rs / tokio

A runtime for writing reliable asynchronous applications with Rust. Provides I/O, networking, scheduling, timers, ...
https://tokio.rs
MIT License
26.59k stars 2.45k forks source link

sync: add watch::Receiver::wait_for_map #6872

Closed Moggers closed 1 week ago

Moggers commented 1 week ago

I'd like the ability to have the value borrowed from a sync::watch::Receiver be discriminated when doing a wait_for so that the caller does not have to repeat that discrimination (eg. if in the process of waiting for an Option to be Some - it'd be nice not to then have to extract the Some from the Option again when actually getting the data out of the watch).

Describe the solution you'd like wait_for_map as described in the original wait_for request seems reasonable. With the Option variants being the morale equivalent of the bool variants in wait_for.

Describe alternatives you've considered

Additional context The code in question is a watch that may contain None - the code wait_fors the value to be Some and then fetches it - receiving an Option that it needs to extract a value from.

I had a quick look at doing this myself thinking it was probably trivial - the apparent lack of a map on parking_lot::RwLockGuard seems to imply its NOT trivial to just discriminate the inner value while maintaining the borrow.

Darksonn commented 1 week ago

I'm sorry, but we won't add this. Like you noticed yourself, it's non-trivial to implement, and you can repeat the logic yourself by wrapping the unwrapping logic in a function call.