Skip to content

Commit

Permalink
Rollup merge of rust-lang#50545 - rizakrko:const_time, r=oli-obk
Browse files Browse the repository at this point in the history
Made some functions in time module const

They may be const, or i missed something?
  • Loading branch information
alexcrichton authored May 11, 2018
2 parents 268b038 + 4d8d0a6 commit d9b6926
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 6 deletions.
42 changes: 40 additions & 2 deletions src/libcore/tests/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,43 @@ fn creation() {
#[test]
fn secs() {
assert_eq!(Duration::new(0, 0).as_secs(), 0);
assert_eq!(Duration::new(0, 500_000_005).as_secs(), 0);
assert_eq!(Duration::new(0, 1_050_000_001).as_secs(), 1);
assert_eq!(Duration::from_secs(1).as_secs(), 1);
assert_eq!(Duration::from_millis(999).as_secs(), 0);
assert_eq!(Duration::from_millis(1001).as_secs(), 1);
assert_eq!(Duration::from_micros(999_999).as_secs(), 0);
assert_eq!(Duration::from_micros(1_000_001).as_secs(), 1);
assert_eq!(Duration::from_nanos(999_999_999).as_secs(), 0);
assert_eq!(Duration::from_nanos(1_000_000_001).as_secs(), 1);
}

#[test]
fn millis() {
assert_eq!(Duration::new(0, 0).subsec_millis(), 0);
assert_eq!(Duration::new(0, 500_000_005).subsec_millis(), 500);
assert_eq!(Duration::new(0, 1_050_000_001).subsec_millis(), 50);
assert_eq!(Duration::from_secs(1).subsec_millis(), 0);
assert_eq!(Duration::from_millis(999).subsec_millis(), 999);
assert_eq!(Duration::from_millis(1001).subsec_millis(), 1);
assert_eq!(Duration::from_micros(999_999).subsec_millis(), 999);
assert_eq!(Duration::from_micros(1_001_000).subsec_millis(), 1);
assert_eq!(Duration::from_nanos(999_999_999).subsec_millis(), 999);
assert_eq!(Duration::from_nanos(1_001_000_000).subsec_millis(), 1);
}

#[test]
fn micros() {
assert_eq!(Duration::new(0, 0).subsec_micros(), 0);
assert_eq!(Duration::new(0, 500_000_005).subsec_micros(), 500_000);
assert_eq!(Duration::new(0, 1_050_000_001).subsec_micros(), 50_000);
assert_eq!(Duration::from_secs(1).subsec_micros(), 0);
assert_eq!(Duration::from_millis(999).subsec_micros(), 999_000);
assert_eq!(Duration::from_millis(1001).subsec_micros(), 1_000);
assert_eq!(Duration::from_micros(999_999).subsec_micros(), 999_999);
assert_eq!(Duration::from_micros(1_000_001).subsec_micros(), 1);
assert_eq!(Duration::from_nanos(999_999_999).subsec_micros(), 999_999);
assert_eq!(Duration::from_nanos(1_000_001_000).subsec_micros(), 1);
}

#[test]
Expand All @@ -34,8 +68,12 @@ fn nanos() {
assert_eq!(Duration::new(0, 5).subsec_nanos(), 5);
assert_eq!(Duration::new(0, 1_000_000_001).subsec_nanos(), 1);
assert_eq!(Duration::from_secs(1).subsec_nanos(), 0);
assert_eq!(Duration::from_millis(999).subsec_nanos(), 999 * 1_000_000);
assert_eq!(Duration::from_millis(1001).subsec_nanos(), 1 * 1_000_000);
assert_eq!(Duration::from_millis(999).subsec_nanos(), 999_000_000);
assert_eq!(Duration::from_millis(1001).subsec_nanos(), 1_000_000);
assert_eq!(Duration::from_micros(999_999).subsec_nanos(), 999_999_000);
assert_eq!(Duration::from_micros(1_000_001).subsec_nanos(), 1000);
assert_eq!(Duration::from_nanos(999_999_999).subsec_nanos(), 999_999_999);
assert_eq!(Duration::from_nanos(1_000_000_001).subsec_nanos(), 1);
}

#[test]
Expand Down
12 changes: 8 additions & 4 deletions src/libcore/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,9 @@ impl Duration {
///
/// [`subsec_nanos`]: #method.subsec_nanos
#[stable(feature = "duration", since = "1.3.0")]
#[rustc_const_unstable(feature="duration_getters")]
#[inline]
pub fn as_secs(&self) -> u64 { self.secs }
pub const fn as_secs(&self) -> u64 { self.secs }

/// Returns the fractional part of this `Duration`, in milliseconds.
///
Expand All @@ -222,8 +223,9 @@ impl Duration {
/// assert_eq!(duration.subsec_millis(), 432);
/// ```
#[stable(feature = "duration_extras", since = "1.27.0")]
#[rustc_const_unstable(feature="duration_getters")]
#[inline]
pub fn subsec_millis(&self) -> u32 { self.nanos / NANOS_PER_MILLI }
pub const fn subsec_millis(&self) -> u32 { self.nanos / NANOS_PER_MILLI }

/// Returns the fractional part of this `Duration`, in microseconds.
///
Expand All @@ -241,8 +243,9 @@ impl Duration {
/// assert_eq!(duration.subsec_micros(), 234_567);
/// ```
#[stable(feature = "duration_extras", since = "1.27.0")]
#[rustc_const_unstable(feature="duration_getters")]
#[inline]
pub fn subsec_micros(&self) -> u32 { self.nanos / NANOS_PER_MICRO }
pub const fn subsec_micros(&self) -> u32 { self.nanos / NANOS_PER_MICRO }

/// Returns the fractional part of this `Duration`, in nanoseconds.
///
Expand All @@ -260,8 +263,9 @@ impl Duration {
/// assert_eq!(duration.subsec_nanos(), 10_000_000);
/// ```
#[stable(feature = "duration", since = "1.3.0")]
#[rustc_const_unstable(feature="duration_getters")]
#[inline]
pub fn subsec_nanos(&self) -> u32 { self.nanos }
pub const fn subsec_nanos(&self) -> u32 { self.nanos }

/// Checked `Duration` addition. Computes `self + other`, returning [`None`]
/// if overflow occurred.
Expand Down
28 changes: 28 additions & 0 deletions src/test/ui/const-eval/duration_conversion.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// compile-pass

#![feature(duration_getters)]

use std::time::Duration;

fn main() {
const _ONE_SECOND: Duration = Duration::from_nanos(1_000_000_000);
const _ONE_MILLISECOND: Duration = Duration::from_nanos(1_000_000);
const _ONE_MICROSECOND: Duration = Duration::from_nanos(1_000);
const _ONE_NANOSECOND: Duration = Duration::from_nanos(1);
const _ONE: usize = _ONE_SECOND.as_secs() as usize;
const _TWO: usize = _ONE_MILLISECOND.subsec_millis() as usize;
const _THREE: usize = _ONE_MICROSECOND.subsec_micros() as usize;
const _FOUR: usize = _ONE_NANOSECOND.subsec_nanos() as usize;
const _0: [[u8; _ONE]; _TWO] = [[1; _ONE]; _TWO];
const _1: [[u8; _THREE]; _FOUR] = [[3; _THREE]; _FOUR];
}

0 comments on commit d9b6926

Please sign in to comment.