Skip to content

Commit

Permalink
Allow more integer types when creating Instants
Browse files Browse the repository at this point in the history
This allows any type that can be converted into an `i64` to be used when
creating an Instant. Because this is no longer a concrete type, this
change may break existing code like the following:

    error: attempt to multiply with overflow
       --> src/time.rs:282:37
        |
    282 |         epoc = Instant::from_millis(2085955200 * 1000).into();
        |                                     ^^^^^^^^^^^^^^^^^
        |
        = note: #[deny(const_err)] on by default

Closes: #272
Approved by: whitequark
  • Loading branch information
crawford authored and Homu committed Jan 27, 2019
1 parent ed8dce0 commit d381dcd
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ pub struct Instant {

impl Instant {
/// Create a new `Instant` from a number of milliseconds.
pub fn from_millis(millis: i64) -> Instant {
Instant { millis }
pub fn from_millis<T: Into<i64>>(millis: T) -> Instant {
Instant { millis: millis.into() }
}

/// Create a new `Instant` from a number of seconds.
pub fn from_secs(secs: i64) -> Instant {
Instant { millis: secs * 1000 }
pub fn from_secs<T: Into<i64>>(secs: T) -> Instant {
Instant { millis: secs.into() * 1000 }
}

/// Create a new `Instant` from the current [std::time::SystemTime].
Expand Down Expand Up @@ -279,7 +279,7 @@ mod test {
assert_eq!(Instant::from(::std::time::UNIX_EPOCH),
Instant::from_millis(0));
assert_eq!(epoc, ::std::time::UNIX_EPOCH);
epoc = Instant::from_millis(2085955200 * 1000).into();
epoc = Instant::from_millis(2085955200i64 * 1000).into();
assert_eq!(epoc, ::std::time::UNIX_EPOCH + ::std::time::Duration::from_secs(2085955200));
}

Expand Down

0 comments on commit d381dcd

Please sign in to comment.