add more time types helper methods

This commit is contained in:
Nikolay Kim 2021-08-28 10:38:21 +06:00
parent d5019e2577
commit 3c056c49b8
4 changed files with 64 additions and 8 deletions

View file

@ -1,19 +1,19 @@
use std::{cell::RefCell, collections::BTreeMap, rc::Rc, time::Instant};
use crate::framed::State;
use crate::time::sleep;
use crate::time::{sleep, Duration};
use crate::util::HashSet;
pub struct Timer(Rc<RefCell<Inner>>);
struct Inner {
resolution: u64,
resolution: Duration,
current: Option<Instant>,
notifications: BTreeMap<Instant, HashSet<State>>,
}
impl Inner {
fn new(resolution: u64) -> Self {
fn new(resolution: Duration) -> Self {
Inner {
resolution,
current: None,
@ -39,13 +39,13 @@ impl Clone for Timer {
impl Default for Timer {
fn default() -> Self {
Timer::with(1_000)
Timer::new(Duration::from_millis(1_000))
}
}
impl Timer {
/// Create new timer with resolution in milliseconds
pub fn with(resolution: u64) -> Timer {
pub fn new(resolution: Duration) -> Timer {
Timer(Rc::new(RefCell::new(Inner::new(resolution))))
}

View file

@ -311,7 +311,7 @@ impl WsRequest {
let fut = self.config.connector.open_tunnel(head.into(), self.addr);
// set request timeout
let (head, framed) = if !self.config.timeout.is_zero() {
let (head, framed) = if self.config.timeout.non_zero() {
timeout(self.config.timeout, fut)
.await
.map_err(|_| SendRequestError::Timeout)

View file

@ -124,7 +124,7 @@ where
let io = Rc::new(RefCell::new(io));
// slow-request timer
if !config.client_timeout.is_zero() {
if config.client_timeout.non_zero() {
expire += std::time::Duration::from(config.client_timeout);
config.timer_h1.register(expire, expire, &state);
}
@ -536,7 +536,7 @@ where
fn reset_keepalive(&mut self) {
// re-register keep-alive
if self.flags.contains(Flags::KEEPALIVE) && !self.config.keep_alive.is_zero() {
if self.flags.contains(Flags::KEEPALIVE) && self.config.keep_alive.non_zero() {
let expire = self.config.timer_h1.now()
+ std::time::Duration::from(self.config.keep_alive);
if expire != self.expire {

View file

@ -1,12 +1,30 @@
use std::convert::TryInto;
// /// A measurement of a monotonically nondecreasing clock. Opaque and useful only with Duration.
// ///
// /// Instants are always guaranteed to be no less than any previously
// /// measured instant when created, and are often useful for tasks such as measuring
// /// benchmarks or timing how long an operation takes.
// #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
// pub struct Instant(u64);
// impl Instant {
// pub fn now() -> Instant {
// todo!()
// }
// }
/// A Duration type to represent a span of time.
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Duration(pub(super) u64);
impl Duration {
/// Zero milliseconds value
pub const ZERO: Duration = Duration(0);
/// One second value
pub const ONE_SEC: Duration = Duration(1_000);
#[inline]
pub const fn from_secs(secs: u32) -> Duration {
Duration((secs as u64) * 1000)
@ -22,6 +40,11 @@ impl Duration {
self.0 == 0
}
#[inline]
pub const fn non_zero(self) -> bool {
self.0 != 0
}
/// Call function `f` if duration is none zero.
#[inline]
pub fn map<F, R>(&self, f: F) -> Option<R>
@ -36,6 +59,13 @@ impl Duration {
}
}
impl Default for Duration {
#[inline]
fn default() -> Duration {
Duration::ZERO
}
}
impl From<u64> for Duration {
#[inline]
fn from(millis: u64) -> Duration {
@ -72,13 +102,32 @@ impl From<Duration> for std::time::Duration {
pub struct Seconds(pub u16);
impl Seconds {
/// Zero seconds value
pub const ZERO: Seconds = Seconds(0);
/// One second value
pub const ONE: Seconds = Seconds(1);
#[inline]
pub const fn new(secs: u16) -> Seconds {
Seconds(secs)
}
#[inline]
pub const fn is_zero(self) -> bool {
self.0 == 0
}
#[inline]
pub const fn non_zero(self) -> bool {
self.0 != 0
}
#[inline]
pub const fn seconds(self) -> u64 {
self.0 as u64
}
/// Call function `f` if seconds is none zero.
#[inline]
pub fn map<F, R>(&self, f: F) -> Option<R>
@ -93,6 +142,13 @@ impl Seconds {
}
}
impl Default for Seconds {
#[inline]
fn default() -> Seconds {
Seconds::ZERO
}
}
impl From<Seconds> for std::time::Duration {
#[inline]
fn from(d: Seconds) -> std::time::Duration {