Skip to content

Commit

Permalink
time: make Interval::poll_tick() public (#3316)
Browse files Browse the repository at this point in the history
  • Loading branch information
seanmonstar committed Dec 22, 2020
1 parent 2ee9520 commit be9fdb6
Showing 1 changed file with 27 additions and 16 deletions.
43 changes: 27 additions & 16 deletions tokio/src/time/interval.rs
Expand Up @@ -117,22 +117,6 @@ pub struct Interval {
}

impl Interval {
fn poll_tick(&mut self, cx: &mut Context<'_>) -> Poll<Instant> {
// Wait for the delay to be done
ready!(Pin::new(&mut self.delay).poll(cx));

// Get the `now` by looking at the `delay` deadline
let now = self.delay.deadline();

// The next interval value is `duration` after the one that just
// yielded.
let next = now + self.period;
self.delay.as_mut().reset(next);

// Return the current instant
Poll::Ready(now)
}

/// Completes when the next instant in the interval has been reached.
///
/// # Examples
Expand All @@ -156,4 +140,31 @@ impl Interval {
pub async fn tick(&mut self) -> Instant {
poll_fn(|cx| self.poll_tick(cx)).await
}

/// Poll for the next instant in the interval to be reached.
///
/// This method can return the following values:
///
/// * `Poll::Pending` if the next instant has not yet been reached.
/// * `Poll::Ready(instant)` if the next instant has been reached.
///
/// When this method returns `Poll::Pending`, the current task is scheduled
/// to receive a wakeup when the instant has elapsed. Note that on multiple
/// calls to `poll_tick`, only the `Waker` from the `Context` passed to the
/// most recent call is scheduled to receive a wakeup.
pub fn poll_tick(&mut self, cx: &mut Context<'_>) -> Poll<Instant> {
// Wait for the delay to be done
ready!(Pin::new(&mut self.delay).poll(cx));

// Get the `now` by looking at the `delay` deadline
let now = self.delay.deadline();

// The next interval value is `duration` after the one that just
// yielded.
let next = now + self.period;
self.delay.as_mut().reset(next);

// Return the current instant
Poll::Ready(now)
}
}

0 comments on commit be9fdb6

Please sign in to comment.