Skip to content

Commit

Permalink
time: move error types into time::error (#2938)
Browse files Browse the repository at this point in the history
  • Loading branch information
alce committed Oct 12, 2020
1 parent ec99e61 commit 0893841
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 49 deletions.
4 changes: 2 additions & 2 deletions tokio-util/src/time/delay_queue.rs
Expand Up @@ -7,7 +7,7 @@
use crate::time::wheel::{self, Wheel};

use futures_core::ready;
use tokio::time::{sleep_until, Duration, Error, Instant, Sleep};
use tokio::time::{error::Error, sleep_until, Duration, Instant, Sleep};

use slab::Slab;
use std::cmp;
Expand Down Expand Up @@ -67,7 +67,7 @@ use std::task::{self, Poll};
/// Using `DelayQueue` to manage cache entries.
///
/// ```rust,no_run
/// use tokio::time::Error;
/// use tokio::time::error::Error;
/// use tokio_util::time::{DelayQueue, delay_queue};
///
/// use futures::ready;
Expand Down
2 changes: 1 addition & 1 deletion tokio/src/stream/timeout.rs
@@ -1,5 +1,5 @@
use crate::stream::{Fuse, Stream};
use crate::time::{Elapsed, Instant, Sleep};
use crate::time::{error::Elapsed, Instant, Sleep};

use core::future::Future;
use core::pin::Pin;
Expand Down
2 changes: 1 addition & 1 deletion tokio/src/time/driver/atomic_stack.rs
@@ -1,5 +1,5 @@
use crate::time::driver::Entry;
use crate::time::Error;
use crate::time::error::Error;

use std::ptr;
use std::sync::atomic::AtomicPtr;
Expand Down
2 changes: 1 addition & 1 deletion tokio/src/time/driver/entry.rs
@@ -1,7 +1,7 @@
use crate::loom::sync::atomic::AtomicU64;
use crate::sync::AtomicWaker;
use crate::time::driver::{Handle, Inner};
use crate::time::{Duration, Error, Instant};
use crate::time::{error::Error, Duration, Instant};

use std::cell::UnsafeCell;
use std::ptr;
Expand Down
4 changes: 2 additions & 2 deletions tokio/src/time/driver/mod.rs
Expand Up @@ -11,7 +11,7 @@ pub(crate) use self::handle::Handle;

use crate::loom::sync::atomic::{AtomicU64, AtomicUsize};
use crate::park::{Park, Unpark};
use crate::time::{wheel, Error};
use crate::time::{error::Error, wheel};
use crate::time::{Clock, Duration, Instant};

use std::sync::atomic::Ordering::{Acquire, Relaxed, Release, SeqCst};
Expand Down Expand Up @@ -204,7 +204,7 @@ where

/// Fires the entry if it needs to, otherwise queue it to be processed later.
fn add_entry(&mut self, entry: Arc<Entry>, when: u64) {
use crate::time::wheel::InsertError;
use crate::time::error::InsertError;

entry.set_when_internal(Some(when));

Expand Down
36 changes: 36 additions & 0 deletions tokio/src/time/error.rs
@@ -1,3 +1,5 @@
//! Time error types.

use self::Kind::*;
use std::error;
use std::fmt;
Expand Down Expand Up @@ -32,6 +34,18 @@ enum Kind {
Invalid = 3,
}

/// Error returned by `Timeout`.
#[derive(Debug, PartialEq)]
pub struct Elapsed(());

#[derive(Debug)]
pub(crate) enum InsertError {
Elapsed,
Invalid,
}

// ===== impl Error =====

impl Error {
/// Creates an error representing a shutdown timer.
pub fn shutdown() -> Error {
Expand Down Expand Up @@ -90,3 +104,25 @@ impl fmt::Display for Error {
write!(fmt, "{}", descr)
}
}

// ===== impl Elapsed =====

impl Elapsed {
pub(crate) fn new() -> Self {
Elapsed(())
}
}

impl fmt::Display for Elapsed {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
"deadline has elapsed".fmt(fmt)
}
}

impl std::error::Error for Elapsed {}

impl From<Elapsed> for std::io::Error {
fn from(_err: Elapsed) -> std::io::Error {
std::io::ErrorKind::TimedOut.into()
}
}
5 changes: 2 additions & 3 deletions tokio/src/time/mod.rs
Expand Up @@ -98,8 +98,7 @@ pub use sleep::{sleep, sleep_until, Sleep};

pub(crate) mod driver;

mod error;
pub use error::Error;
pub mod error;

mod instant;
pub use self::instant::Instant;
Expand All @@ -109,7 +108,7 @@ pub use interval::{interval, interval_at, Interval};

mod timeout;
#[doc(inline)]
pub use timeout::{timeout, timeout_at, Elapsed, Timeout};
pub use timeout::{timeout, timeout_at, Timeout};

mod wheel;

Expand Down
2 changes: 1 addition & 1 deletion tokio/src/time/sleep.rs
@@ -1,5 +1,5 @@
use crate::time::driver::{Entry, Handle};
use crate::time::{Duration, Error, Instant};
use crate::time::{error::Error, Duration, Instant};

use std::future::Future;
use std::pin::Pin;
Expand Down
33 changes: 2 additions & 31 deletions tokio/src/time/timeout.rs
Expand Up @@ -4,10 +4,9 @@
//!
//! [`Timeout`]: struct@Timeout

use crate::time::{sleep_until, Duration, Instant, Sleep};
use crate::time::{error::Elapsed, sleep_until, Duration, Instant, Sleep};

use pin_project_lite::pin_project;
use std::fmt;
use std::future::Future;
use std::pin::Pin;
use std::task::{self, Poll};
Expand Down Expand Up @@ -112,18 +111,6 @@ pin_project! {
}
}

/// Error returned by `Timeout`.
#[derive(Debug, PartialEq)]
pub struct Elapsed(());

impl Elapsed {
// Used on StreamExt::timeout
#[allow(unused)]
pub(crate) fn new() -> Self {
Elapsed(())
}
}

impl<T> Timeout<T> {
pub(crate) fn new_with_delay(value: T, delay: Sleep) -> Timeout<T> {
Timeout { value, delay }
Expand Down Expand Up @@ -161,24 +148,8 @@ where

// Now check the timer
match me.delay.poll(cx) {
Poll::Ready(()) => Poll::Ready(Err(Elapsed(()))),
Poll::Ready(()) => Poll::Ready(Err(Elapsed::new())),
Poll::Pending => Poll::Pending,
}
}
}

// ===== impl Elapsed =====

impl fmt::Display for Elapsed {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
"deadline has elapsed".fmt(fmt)
}
}

impl std::error::Error for Elapsed {}

impl From<Elapsed> for std::io::Error {
fn from(_err: Elapsed) -> std::io::Error {
std::io::ErrorKind::TimedOut.into()
}
}
8 changes: 1 addition & 7 deletions tokio/src/time/wheel/mod.rs
@@ -1,4 +1,4 @@
use crate::time::driver::Entry;
use crate::time::{driver::Entry, error::InsertError};

mod level;
pub(crate) use self::level::Expiration;
Expand Down Expand Up @@ -50,12 +50,6 @@ const NUM_LEVELS: usize = 6;
/// The maximum duration of a `Sleep`
const MAX_DURATION: u64 = (1 << (6 * NUM_LEVELS)) - 1;

#[derive(Debug)]
pub(crate) enum InsertError {
Elapsed,
Invalid,
}

impl Wheel {
/// Create a new timing wheel
pub(crate) fn new() -> Wheel {
Expand Down

0 comments on commit 0893841

Please sign in to comment.