Skip to content

Commit

Permalink
deprecate delay_for
Browse files Browse the repository at this point in the history
  • Loading branch information
alce committed Sep 8, 2020
1 parent cc3914c commit a59421d
Show file tree
Hide file tree
Showing 31 changed files with 134 additions and 132 deletions.
4 changes: 2 additions & 2 deletions tokio-test/src/io.rs
Expand Up @@ -364,7 +364,7 @@ impl AsyncRead for Mock {
Err(ref e) if e.kind() == io::ErrorKind::WouldBlock => {
if let Some(rem) = self.inner.remaining_wait() {
let until = Instant::now() + rem;
self.inner.sleep = Some(time::delay_until(until));
self.inner.sleep = Some(time::sleep_until(until));
} else {
self.inner.read_wait = Some(cx.waker().clone());
return Poll::Pending;
Expand Down Expand Up @@ -409,7 +409,7 @@ impl AsyncWrite for Mock {
Err(ref e) if e.kind() == io::ErrorKind::WouldBlock => {
if let Some(rem) = self.inner.remaining_wait() {
let until = Instant::now() + rem;
self.inner.sleep = Some(time::delay_until(until));
self.inner.sleep = Some(time::sleep_until(until));
} else {
panic!("unexpected WouldBlock");
}
Expand Down
4 changes: 2 additions & 2 deletions tokio-test/tests/block_on.rs
@@ -1,6 +1,6 @@
#![warn(rust_2018_idioms)]

use tokio::time::{delay_until, Duration, Instant};
use tokio::time::{sleep_until, Duration, Instant};
use tokio_test::block_on;

#[test]
Expand All @@ -22,6 +22,6 @@ fn test_delay() {
let deadline = Instant::now() + Duration::from_millis(100);

block_on(async {
delay_until(deadline).await;
sleep_until(deadline).await;
});
}
6 changes: 3 additions & 3 deletions tokio-util/src/context.rs
Expand Up @@ -46,7 +46,7 @@ pub trait RuntimeExt {
///
/// ```no_run
/// use tokio_util::context::RuntimeExt;
/// use tokio::time::{delay_for, Duration};
/// use tokio::time::{sleep, Duration};
///
/// let rt = tokio::runtime::Builder::new()
/// .threaded_scheduler()
Expand All @@ -57,11 +57,11 @@ pub trait RuntimeExt {
/// .threaded_scheduler()
/// .build().unwrap();
///
/// let fut = delay_for(Duration::from_millis(2));
/// let fut = sleep(Duration::from_millis(2));
///
/// rt.block_on(
/// rt2
/// .wrap(async { delay_for(Duration::from_millis(2)).await }),
/// .wrap(async { sleep(Duration::from_millis(2)).await }),
/// );
///```
fn wrap<F: Future>(&self, fut: F) -> TokioContext<'_, F>;
Expand Down
8 changes: 4 additions & 4 deletions tokio-util/src/sync/cancellation_token.rs
Expand Up @@ -37,14 +37,14 @@ use core::task::{Context, Poll, Waker};
/// // The token was cancelled
/// 5
/// }
/// _ = tokio::time::delay_for(std::time::Duration::from_secs(9999)) => {
/// _ = tokio::time::sleep(std::time::Duration::from_secs(9999)) => {
/// 99
/// }
/// }
/// });
///
/// tokio::spawn(async move {
/// tokio::time::delay_for(std::time::Duration::from_millis(10)).await;
/// tokio::time::sleep(std::time::Duration::from_millis(10)).await;
/// token.cancel();
/// });
///
Expand Down Expand Up @@ -185,14 +185,14 @@ impl CancellationToken {
/// // The token was cancelled
/// 5
/// }
/// _ = tokio::time::delay_for(std::time::Duration::from_secs(9999)) => {
/// _ = tokio::time::sleep(std::time::Duration::from_secs(9999)) => {
/// 99
/// }
/// }
/// });
///
/// tokio::spawn(async move {
/// tokio::time::delay_for(std::time::Duration::from_millis(10)).await;
/// tokio::time::sleep(std::time::Duration::from_millis(10)).await;
/// token.cancel();
/// });
///
Expand Down
2 changes: 1 addition & 1 deletion tokio-util/tests/context.rs
Expand Up @@ -21,5 +21,5 @@ fn tokio_context_with_another_runtime() {

// Without the `HandleExt.wrap()` there would be a panic because there is
// no timer running, since it would be referencing runtime r1.
let _ = rt1.block_on(rt2.wrap(async move { delay_for(Duration::from_millis(2)).await }));
let _ = rt1.block_on(rt2.wrap(async move { sleep(Duration::from_millis(2)).await }));
}
2 changes: 1 addition & 1 deletion tokio/src/lib.rs
Expand Up @@ -190,7 +190,7 @@
//! In order to use `tokio::time`, the "time" feature flag must be enabled.
//!
//! [`tokio::time`]: crate::time
//! [delay]: crate::time::delay_for()
//! [delay]: crate::time::sleep()
//! [interval]: crate::time::interval()
//! [timeout]: crate::time::timeout()
//!
Expand Down
8 changes: 4 additions & 4 deletions tokio/src/macros/select.rs
Expand Up @@ -76,7 +76,7 @@
///
/// #[tokio::main]
/// async fn main() {
/// let mut delay = time::delay_for(Duration::from_millis(50));
/// let mut delay = time::sleep(Duration::from_millis(50));
///
/// while !delay.is_elapsed() {
/// tokio::select! {
Expand All @@ -103,13 +103,13 @@
/// use tokio::time::{self, Duration};
///
/// async fn some_async_work() {
/// # time::delay_for(Duration::from_millis(10)).await;
/// # time::sleep(Duration::from_millis(10)).await;
/// // do work
/// }
///
/// #[tokio::main]
/// async fn main() {
/// let mut delay = time::delay_for(Duration::from_millis(50));
/// let mut delay = time::sleep(Duration::from_millis(50));
///
/// loop {
/// tokio::select! {
Expand Down Expand Up @@ -226,7 +226,7 @@
/// #[tokio::main]
/// async fn main() {
/// let mut stream = stream::iter(vec![1, 2, 3]);
/// let mut delay = time::delay_for(Duration::from_secs(1));
/// let mut delay = time::sleep(Duration::from_secs(1));
///
/// loop {
/// tokio::select! {
Expand Down
4 changes: 2 additions & 2 deletions tokio/src/runtime/task/join.rs
Expand Up @@ -121,7 +121,7 @@ doc_rt_core! {
/// let original_task = task::spawn(async {
/// let _detached_task = task::spawn(async {
/// // Here we sleep to make sure that the first task returns before.
/// time::delay_for(Duration::from_millis(10)).await;
/// time::sleep(Duration::from_millis(10)).await;
/// // This will be called, even though the JoinHandle is dropped.
/// println!("♫ Still alive ♫");
/// });
Expand All @@ -133,7 +133,7 @@ doc_rt_core! {
/// // We make sure that the new task has time to run, before the main
/// // task returns.
///
/// time::delay_for(Duration::from_millis(1000)).await;
/// time::sleep(Duration::from_millis(1000)).await;
/// # }
/// ```
///
Expand Down
6 changes: 3 additions & 3 deletions tokio/src/stream/mod.rs
Expand Up @@ -231,18 +231,18 @@ pub trait StreamExt: Stream {
/// tx1.send(2).await.unwrap();
///
/// // Let the other task send values
/// time::delay_for(Duration::from_millis(20)).await;
/// time::sleep(Duration::from_millis(20)).await;
///
/// tx1.send(4).await.unwrap();
/// });
///
/// tokio::spawn(async move {
/// // Wait for the first task to send values
/// time::delay_for(Duration::from_millis(5)).await;
/// time::sleep(Duration::from_millis(5)).await;
///
/// tx2.send(3).await.unwrap();
///
/// time::delay_for(Duration::from_millis(25)).await;
/// time::sleep(Duration::from_millis(25)).await;
///
/// // Send the final value
/// tx2.send(5).await.unwrap();
Expand Down
6 changes: 3 additions & 3 deletions tokio/src/sync/mod.rs
Expand Up @@ -322,7 +322,7 @@
//! tokio::spawn(async move {
//! loop {
//! // Wait 10 seconds between checks
//! time::delay_for(Duration::from_secs(10)).await;
//! time::sleep(Duration::from_secs(10)).await;
//!
//! // Load the configuration file
//! let new_config = Config::load_from_file().await.unwrap();
Expand Down Expand Up @@ -361,7 +361,7 @@
//! let mut conf = rx.recv().await;
//!
//! let mut op_start = Instant::now();
//! let mut delay = time::delay_until(op_start + conf.timeout);
//! let mut delay = time::sleep_until(op_start + conf.timeout);
//!
//! loop {
//! tokio::select! {
Expand All @@ -373,7 +373,7 @@
//! op_start = Instant::now();
//!
//! // Restart the timeout
//! delay = time::delay_until(op_start + conf.timeout);
//! delay = time::sleep_until(op_start + conf.timeout);
//! }
//! new_conf = rx.recv() => {
//! conf = new_conf;
Expand Down
4 changes: 2 additions & 2 deletions tokio/src/sync/mpsc/bounded.rs
Expand Up @@ -382,7 +382,7 @@ impl<T> Sender<T> {
///
/// ```rust
/// use tokio::sync::mpsc;
/// use tokio::time::{delay_for, Duration};
/// use tokio::time::{sleep, Duration};
///
/// #[tokio::main]
/// async fn main() {
Expand All @@ -399,7 +399,7 @@ impl<T> Sender<T> {
///
/// while let Some(i) = rx.recv().await {
/// println!("got = {}", i);
/// delay_for(Duration::from_millis(200)).await;
/// sleep(Duration::from_millis(200)).await;
/// }
/// }
/// ```
Expand Down
2 changes: 1 addition & 1 deletion tokio/src/task/local.rs
Expand Up @@ -95,7 +95,7 @@ cfg_rt_util! {
/// });
///
/// local.spawn_local(async move {
/// time::delay_for(time::Duration::from_millis(100)).await;
/// time::sleep(time::Duration::from_millis(100)).await;
/// println!("goodbye {}", unsend_data)
/// });
///
Expand Down
3 changes: 2 additions & 1 deletion tokio/src/time/delay.rs
Expand Up @@ -60,7 +60,8 @@ pub fn sleep(duration: Duration) -> Delay {
sleep_until(Instant::now() + duration)
}

/// Waits until `duration` has elapsed. Alias for [`sleep`](sleep).
/// Waits until `duration` has elapsed.
#[deprecated(note = "delay_for will be removed in Tokio 0.3. Use time::sleep")]
pub fn delay_for(duration: Duration) -> Delay {
sleep(duration)
}
Expand Down
10 changes: 5 additions & 5 deletions tokio/src/time/delay_queue.rs
Expand Up @@ -5,7 +5,7 @@
//! [`DelayQueue`]: struct@DelayQueue

use crate::time::wheel::{self, Wheel};
use crate::time::{delay_until, Delay, Duration, Error, Instant};
use crate::time::{sleep_until, Delay, Duration, Error, Instant};

use slab::Slab;
use std::cmp;
Expand Down Expand Up @@ -51,7 +51,7 @@ use std::task::{self, Poll};
/// # Implementation
///
/// The [`DelayQueue`] is backed by a separate instance of the same timer wheel used internally by
/// Tokio's standalone timer utilities such as [`delay_for`]. Because of this, it offers the same
/// Tokio's standalone timer utilities such as [`sleep`]. Because of this, it offers the same
/// performance and scalability benefits.
///
/// State associated with each entry is stored in a [`slab`]. This amortizes the cost of allocation,
Expand Down Expand Up @@ -118,7 +118,7 @@ use std::task::{self, Poll};
/// [`poll_expired`]: method@Self::poll_expired
/// [`Stream::poll_expired`]: method@Self::poll_expired
/// [`DelayQueue`]: struct@DelayQueue
/// [`delay_for`]: fn@super::delay_for
/// [`sleep`]: fn@super::sleep
/// [`slab`]: slab
/// [`capacity`]: method@Self::capacity
/// [`reserve`]: method@Self::reserve
Expand Down Expand Up @@ -330,7 +330,7 @@ impl<T> DelayQueue<T> {
if let Some(ref mut delay) = &mut self.delay {
delay.reset(delay_time);
} else {
self.delay = Some(delay_until(delay_time));
self.delay = Some(sleep_until(delay_time));
}
}

Expand Down Expand Up @@ -732,7 +732,7 @@ impl<T> DelayQueue<T> {
// We poll the wheel to get the next value out before finding the next deadline.
let wheel_idx = self.wheel.poll(&mut self.poll, &mut self.slab);

self.delay = self.next_deadline().map(delay_until);
self.delay = self.next_deadline().map(sleep_until);

if let Some(idx) = wheel_idx {
return Poll::Ready(Some(Ok(idx)));
Expand Down
4 changes: 2 additions & 2 deletions tokio/src/time/driver/handle.rs
Expand Up @@ -25,9 +25,9 @@ impl Handle {
/// `Builder::enable_all()` are not included in the builder.
///
/// It can also panic whenever a timer is created outside of a Tokio
/// runtime. That is why `rt.block_on(delay_for(...))` will panic,
/// runtime. That is why `rt.block_on(sleep(...))` will panic,
/// since the function is executed outside of the runtime.
/// Whereas `rt.block_on(async {delay_for(...).await})` doesn't
/// Whereas `rt.block_on(async {sleep(...).await})` doesn't
/// panic. And this is because wrapping the function on an async makes it
/// lazy, and so gets executed inside the runtime successfuly without
/// panicking.
Expand Down
12 changes: 6 additions & 6 deletions tokio/src/time/instant.rs
Expand Up @@ -50,12 +50,12 @@ impl Instant {
/// # Examples
///
/// ```
/// use tokio::time::{Duration, Instant, delay_for};
/// use tokio::time::{Duration, Instant, sleep};
///
/// #[tokio::main]
/// async fn main() {
/// let now = Instant::now();
/// delay_for(Duration::new(1, 0)).await;
/// sleep(Duration::new(1, 0)).await;
/// let new_now = Instant::now();
/// println!("{:?}", new_now.checked_duration_since(now));
/// println!("{:?}", now.checked_duration_since(new_now)); // None
Expand All @@ -71,12 +71,12 @@ impl Instant {
/// # Examples
///
/// ```
/// use tokio::time::{Duration, Instant, delay_for};
/// use tokio::time::{Duration, Instant, sleep};
///
/// #[tokio::main]
/// async fn main() {
/// let now = Instant::now();
/// delay_for(Duration::new(1, 0)).await;
/// sleep(Duration::new(1, 0)).await;
/// let new_now = Instant::now();
/// println!("{:?}", new_now.saturating_duration_since(now));
/// println!("{:?}", now.saturating_duration_since(new_now)); // 0ns
Expand All @@ -97,13 +97,13 @@ impl Instant {
/// # Examples
///
/// ```
/// use tokio::time::{Duration, Instant, delay_for};
/// use tokio::time::{Duration, Instant, sleep};
///
/// #[tokio::main]
/// async fn main() {
/// let instant = Instant::now();
/// let three_secs = Duration::from_secs(3);
/// delay_for(three_secs).await;
/// sleep(three_secs).await;
/// assert!(instant.elapsed() >= three_secs);
/// }
/// ```
Expand Down
12 changes: 6 additions & 6 deletions tokio/src/time/interval.rs
@@ -1,5 +1,5 @@
use crate::future::poll_fn;
use crate::time::{delay_until, Delay, Duration, Instant};
use crate::time::{sleep_until, Delay, Duration, Instant};

use std::future::Future;
use std::pin::Pin;
Expand Down Expand Up @@ -36,12 +36,12 @@ use std::task::{Context, Poll};
///
/// A simple example using `interval` to execute a task every two seconds.
///
/// The difference between `interval` and [`delay_for`] is that an `interval`
/// The difference between `interval` and [`sleep`] is that an `interval`
/// measures the time since the last tick, which means that `.tick().await`
/// may wait for a shorter time than the duration specified for the interval
/// if some time has passed between calls to `.tick().await`.
///
/// If the tick in the example below was replaced with [`delay_for`], the task
/// If the tick in the example below was replaced with [`sleep`], the task
/// would only be executed once every three seconds, and not every two
/// seconds.
///
Expand All @@ -50,7 +50,7 @@ use std::task::{Context, Poll};
///
/// async fn task_that_takes_a_second() {
/// println!("hello");
/// time::delay_for(time::Duration::from_secs(1)).await
/// time::sleep(time::Duration::from_secs(1)).await
/// }
///
/// #[tokio::main]
Expand All @@ -63,7 +63,7 @@ use std::task::{Context, Poll};
/// }
/// ```
///
/// [`delay_for`]: crate::time::delay_for()
/// [`sleep`]: crate::time::sleep()
pub fn interval(period: Duration) -> Interval {
assert!(period > Duration::new(0, 0), "`period` must be non-zero.");

Expand Down Expand Up @@ -101,7 +101,7 @@ pub fn interval_at(start: Instant, period: Duration) -> Interval {
assert!(period > Duration::new(0, 0), "`period` must be non-zero.");

Interval {
delay: delay_until(start),
delay: sleep_until(start),
period,
}
}
Expand Down

0 comments on commit a59421d

Please sign in to comment.