-
Notifications
You must be signed in to change notification settings - Fork 478
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
Remove debounced dependency #2138
base: master
Are you sure you want to change the base?
Conversation
@@ -149,7 +145,13 @@ impl PrivacyMonitor for Monitor<'_> { | |||
break; | |||
} | |||
}, | |||
_ = self.updates.next() => break, | |||
_ = self.stream.next() => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any thoughts on adding our own stream extensions that could include this pattern (currently used in three blocks)?
impl<T: ?Sized> StreamExt for T where T: Stream {}
#[async_trait]
pub trait StreamExt: Stream {
async fn consume_for(&mut self, duration: Duration)
where
Self: Unpin,
{
let _ = self.next().await;
let _ = tokio::time::timeout(duration, async {
loop {
let _ = self.next().await;
}
})
.await;
}
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@MaxVerevkin if you like the idea, but have a better function name, I'm not too fond of consume_for
.
wait_for_next_then_discard_for_duration
seems a bit too verbose.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think your implementation is not cancel-safe, which may be a problem. Perhaps something like (this does not have a timeout and expects successive items to be available immediately, not sure if it is good enough. on a plus side it does not have a delay 😄 )
pub trait StreamExtDebounced: futures::StreamExt {
fn next_debounced(&mut self) -> impl Future<Output = Option<Self::Item>>;
}
impl<T: futures::StreamExt + Unpin> StreamExtDebounced for T {
async fn next_debounced(&mut self) -> Option<Self::Item> {
let mut result = self.next().await?;
let mut noop_ctx = std::task::Context::from_waker(std::task::Waker::noop());
loop {
match self.poll_next_unpin(&mut noop_ctx) {
std::task::Poll::Ready(Some(x)) => result = x,
std::task::Poll::Ready(None) | std::task::Poll::Pending => return Some(result),
}
}
}
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
std::task::Waker::noop()
is now conveniently stable :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will wait until #2143 is merged so that use std::future::Future;
doesn't need to be imported too.
No description provided.