1
Fork 0

Add Hash impl for SystemTime and Instant

Closes #46670.
This commit is contained in:
Vitaly _Vi Shukela 2017-12-19 00:35:43 +03:00
parent a3a7203e2c
commit 1d5ead453d
No known key found for this signature in database
GPG key ID: C097221D6E03DF68
5 changed files with 34 additions and 11 deletions

View file

@ -13,6 +13,7 @@ use fmt;
use sys::{cvt, syscall}; use sys::{cvt, syscall};
use time::Duration; use time::Duration;
use convert::TryInto; use convert::TryInto;
use core::hash::{Hash, Hasher};
const NSEC_PER_SEC: u64 = 1_000_000_000; const NSEC_PER_SEC: u64 = 1_000_000_000;
@ -110,12 +111,19 @@ impl Ord for Timespec {
} }
} }
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord)] impl Hash for Timespec {
fn hash<H : Hasher>(&self, state: &mut H) {
self.t.tv_sec.hash(state);
self.t.tv_nsec.hash(state);
}
}
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Instant { pub struct Instant {
t: Timespec, t: Timespec,
} }
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord)] #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct SystemTime { pub struct SystemTime {
t: Timespec, t: Timespec,
} }

View file

@ -11,6 +11,7 @@
use cmp::Ordering; use cmp::Ordering;
use libc; use libc;
use time::Duration; use time::Duration;
use core::hash::{Hash, Hasher};
pub use self::inner::{Instant, SystemTime, UNIX_EPOCH}; pub use self::inner::{Instant, SystemTime, UNIX_EPOCH};
use convert::TryInto; use convert::TryInto;
@ -111,6 +112,13 @@ impl Ord for Timespec {
} }
} }
impl Hash for Timespec {
fn hash<H : Hasher>(&self, state: &mut H) {
self.t.tv_sec.hash(state);
self.t.tv_nsec.hash(state);
}
}
#[cfg(any(target_os = "macos", target_os = "ios"))] #[cfg(any(target_os = "macos", target_os = "ios"))]
mod inner { mod inner {
use fmt; use fmt;
@ -123,12 +131,12 @@ mod inner {
use super::NSEC_PER_SEC; use super::NSEC_PER_SEC;
use super::Timespec; use super::Timespec;
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug)] #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
pub struct Instant { pub struct Instant {
t: u64 t: u64
} }
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord)] #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct SystemTime { pub struct SystemTime {
t: Timespec, t: Timespec,
} }
@ -255,12 +263,12 @@ mod inner {
use super::Timespec; use super::Timespec;
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord)] #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Instant { pub struct Instant {
t: Timespec, t: Timespec,
} }
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord)] #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct SystemTime { pub struct SystemTime {
t: Timespec, t: Timespec,
} }

View file

@ -11,10 +11,10 @@
use fmt; use fmt;
use time::Duration; use time::Duration;
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug)] #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
pub struct Instant; pub struct Instant;
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord)] #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct SystemTime; pub struct SystemTime;
pub const UNIX_EPOCH: SystemTime = SystemTime; pub const UNIX_EPOCH: SystemTime = SystemTime;

View file

@ -17,11 +17,12 @@ use sys::cvt;
use sys_common::mul_div_u64; use sys_common::mul_div_u64;
use time::Duration; use time::Duration;
use convert::TryInto; use convert::TryInto;
use core::hash::{Hash, Hasher};
const NANOS_PER_SEC: u64 = 1_000_000_000; const NANOS_PER_SEC: u64 = 1_000_000_000;
const INTERVALS_PER_SEC: u64 = NANOS_PER_SEC / 100; const INTERVALS_PER_SEC: u64 = NANOS_PER_SEC / 100;
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Debug)] #[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Debug, Hash)]
pub struct Instant { pub struct Instant {
t: c::LARGE_INTEGER, t: c::LARGE_INTEGER,
} }
@ -173,6 +174,12 @@ impl From<c::FILETIME> for SystemTime {
} }
} }
impl Hash for SystemTime {
fn hash<H : Hasher>(&self, state: &mut H) {
self.intervals().hash(state)
}
}
fn dur2intervals(d: &Duration) -> i64 { fn dur2intervals(d: &Duration) -> i64 {
d.as_secs() d.as_secs()
.checked_mul(INTERVALS_PER_SEC) .checked_mul(INTERVALS_PER_SEC)

View file

@ -66,7 +66,7 @@ mod duration;
/// println!("{}", now.elapsed().as_secs()); /// println!("{}", now.elapsed().as_secs());
/// } /// }
/// ``` /// ```
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord)] #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[stable(feature = "time2", since = "1.8.0")] #[stable(feature = "time2", since = "1.8.0")]
pub struct Instant(time::Instant); pub struct Instant(time::Instant);
@ -118,7 +118,7 @@ pub struct Instant(time::Instant);
/// } /// }
/// } /// }
/// ``` /// ```
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord)] #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[stable(feature = "time2", since = "1.8.0")] #[stable(feature = "time2", since = "1.8.0")]
pub struct SystemTime(time::SystemTime); pub struct SystemTime(time::SystemTime);