Skip to content

Commit

Permalink
Rollup merge of rust-lang#46828 - vi:hash_for_systemtime_instant, r=d…
Browse files Browse the repository at this point in the history
…tolnay

Add Hash impl for SystemTime and Instant

Closes rust-lang#46670.

Not sure how to actually test non-Linux platforms.
`rustc --target=i686-pc-windows-gnu --crate-name std --crate-type rlib src/libstd/lib.rs -o q.rlib` works a bit, but for Redox I'm not sure what to do.

r? @dtolnay
  • Loading branch information
kennytm committed Dec 20, 2017
2 parents f35bb15 + 1d5ead4 commit 5e98112
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 11 deletions.
12 changes: 10 additions & 2 deletions src/libstd/sys/redox/time.rs
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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

0 comments on commit 5e98112

Please sign in to comment.