time: move error types into time::error (#2938)

This commit is contained in:
Juan Alvarez 2020-10-12 12:21:44 -05:00 committed by GitHub
parent ec99e61945
commit 0893841f31
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 49 additions and 49 deletions

View File

@ -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;
@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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};
@ -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));

View File

@ -1,3 +1,5 @@
//! Time error types.
use self::Kind::*;
use std::error;
use std::fmt;
@ -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 {
@ -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()
}
}

View File

@ -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;
@ -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;

View File

@ -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;

View File

@ -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};
@ -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 }
@ -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()
}
}

View File

@ -1,4 +1,4 @@
use crate::time::driver::Entry;
use crate::time::{driver::Entry, error::InsertError};
mod level;
pub(crate) use self::level::Expiration;
@ -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 {