Skip to content
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

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

Merged
merged 3 commits into from
Oct 12, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion tokio/src/time/driver/mod.rs
Original file line number Diff line number Diff line change
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
34 changes: 34 additions & 0 deletions tokio/src/time/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,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 +102,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()
}
}
4 changes: 2 additions & 2 deletions tokio/src/time/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ pub use sleep::{sleep, sleep_until, Sleep};
pub(crate) mod driver;

mod error;
pub use error::Error;
pub use error::{Elapsed, Error};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This import should be removed. Then we can avoid cluttering the main namespace with error types :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed. Hopefully I understood correctly how they should be exposed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

btw @alce I think he means just doing pub mod error then you can address each error variant via error::Elapsed etc.


mod instant;
pub use self::instant::Instant;
Expand All @@ -109,7 +109,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
33 changes: 2 additions & 31 deletions tokio/src/time/timeout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@
//!
//! [`Timeout`]: struct@Timeout

use crate::time::{sleep_until, Duration, Instant, Sleep};
use crate::time::{sleep_until, Duration, Elapsed, 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
Original file line number Diff line number Diff line change
@@ -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