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

Add Hash impl for SystemTime and Instant #46828

Merged
merged 1 commit into from Dec 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
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
12 changes: 10 additions & 2 deletions src/libstd/sys/redox/time.rs
Expand Up @@ -13,6 +13,7 @@ use fmt;
use sys::{cvt, syscall};
use time::Duration;
use convert::TryInto;
use core::hash::{Hash, Hasher};

const NSEC_PER_SEC: u64 = 1_000_000_000;

Expand Down Expand Up @@ -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 {
t: Timespec,
}

#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct SystemTime {
t: Timespec,
}
Expand Down
16 changes: 12 additions & 4 deletions src/libstd/sys/unix/time.rs
Expand Up @@ -11,6 +11,7 @@
use cmp::Ordering;
use libc;
use time::Duration;
use core::hash::{Hash, Hasher};

pub use self::inner::{Instant, SystemTime, UNIX_EPOCH};
use convert::TryInto;
Expand Down Expand Up @@ -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"))]
mod inner {
use fmt;
Expand All @@ -123,12 +131,12 @@ mod inner {
use super::NSEC_PER_SEC;
use super::Timespec;

#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug)]
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
pub struct Instant {
t: u64
}

#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct SystemTime {
t: Timespec,
}
Expand Down Expand Up @@ -255,12 +263,12 @@ mod inner {

use super::Timespec;

#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Instant {
t: Timespec,
}

#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct SystemTime {
t: Timespec,
}
Expand Down
4 changes: 2 additions & 2 deletions src/libstd/sys/wasm/time.rs
Expand Up @@ -11,10 +11,10 @@
use fmt;
use time::Duration;

#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug)]
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
pub struct Instant;

#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct SystemTime;

pub const UNIX_EPOCH: SystemTime = SystemTime;
Expand Down
9 changes: 8 additions & 1 deletion src/libstd/sys/windows/time.rs
Expand Up @@ -17,11 +17,12 @@ use sys::cvt;
use sys_common::mul_div_u64;
use time::Duration;
use convert::TryInto;
use core::hash::{Hash, Hasher};

const NANOS_PER_SEC: u64 = 1_000_000_000;
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 {
t: c::LARGE_INTEGER,
}
Expand Down Expand Up @@ -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 {
d.as_secs()
.checked_mul(INTERVALS_PER_SEC)
Expand Down
4 changes: 2 additions & 2 deletions src/libstd/time/mod.rs
Expand Up @@ -66,7 +66,7 @@ mod duration;
/// 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")]
pub struct Instant(time::Instant);

Expand Down Expand Up @@ -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")]
pub struct SystemTime(time::SystemTime);

Expand Down