-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add back crux_time::Duration and modify api instead
- Loading branch information
1 parent
f92f93e
commit f6d7dd9
Showing
9 changed files
with
206 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
use serde::{Deserialize, Serialize}; | ||
|
||
use crate::{error::TimeResult, TimeError}; | ||
|
||
/// The number of nanoseconds in seconds. | ||
pub(crate) const NANOS_PER_SEC: u32 = 1_000_000_000; | ||
/// The number of nanoseconds in a millisecond. | ||
const NANOS_PER_MILLI: u32 = 1_000_000; | ||
|
||
/// Represents a duration of time, internally stored as nanoseconds | ||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] | ||
#[serde(rename_all = "camelCase")] | ||
pub struct Duration { | ||
nanos: u64, | ||
} | ||
|
||
impl Duration { | ||
/// Create a new `Duration` from the given number of nanoseconds. | ||
pub fn new(nanos: u64) -> Self { | ||
Self { nanos } | ||
} | ||
|
||
/// Create a new `Duration` from the given number of milliseconds. | ||
/// | ||
/// Errors with [`TimeError::InvalidDuration`] if the number of milliseconds | ||
/// would overflow when converted to nanoseconds. | ||
pub fn from_millis(millis: u64) -> TimeResult<Self> { | ||
let nanos = millis | ||
.checked_mul(NANOS_PER_MILLI as u64) | ||
.ok_or(TimeError::InvalidDuration)?; | ||
Ok(Self { nanos }) | ||
} | ||
|
||
/// Create a new `Duration` from the given number of seconds. | ||
/// | ||
/// Errors with [`TimeError::InvalidDuration`] if the number of seconds | ||
/// would overflow when converted to nanoseconds. | ||
pub fn from_secs(seconds: u64) -> TimeResult<Self> { | ||
let nanos = seconds | ||
.checked_mul(NANOS_PER_SEC as u64) | ||
.ok_or(TimeError::InvalidDuration)?; | ||
Ok(Self { nanos }) | ||
} | ||
} | ||
|
||
impl From<std::time::Duration> for Duration { | ||
fn from(duration: std::time::Duration) -> Self { | ||
Duration { | ||
nanos: duration.as_nanos() as u64, | ||
} | ||
} | ||
} | ||
|
||
impl From<Duration> for std::time::Duration { | ||
fn from(duration: Duration) -> Self { | ||
std::time::Duration::from_nanos(duration.nanos) | ||
} | ||
} | ||
|
||
#[cfg(feature = "chrono")] | ||
impl TryFrom<chrono::TimeDelta> for Duration { | ||
type Error = TimeError; | ||
|
||
fn try_from(value: chrono::TimeDelta) -> Result<Self, Self::Error> { | ||
let nanos = value.num_nanoseconds().ok_or(TimeError::InvalidDuration)? as u64; | ||
Ok(Self { nanos }) | ||
} | ||
} | ||
|
||
#[cfg(feature = "chrono")] | ||
impl TryFrom<Duration> for chrono::TimeDelta { | ||
type Error = TimeError; | ||
|
||
fn try_from(value: Duration) -> Result<Self, Self::Error> { | ||
let nanos = value | ||
.nanos | ||
.try_into() | ||
.map_err(|_| TimeError::InvalidDuration)?; | ||
Ok(chrono::TimeDelta::nanoseconds(nanos)) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use super::Duration; | ||
use std::time::Duration as StdDuration; | ||
|
||
#[test] | ||
fn duration_from_millis() { | ||
let duration = Duration::from_millis(1_000).unwrap(); | ||
assert_eq!(duration.nanos, 1_000_000_000); | ||
} | ||
|
||
#[test] | ||
fn duration_from_secs() { | ||
let duration = Duration::from_secs(1).unwrap(); | ||
assert_eq!(duration.nanos, 1_000_000_000); | ||
} | ||
|
||
#[test] | ||
fn std_into_duration() { | ||
let actual: Duration = StdDuration::from_millis(100).into(); | ||
let expected = Duration { nanos: 100_000_000 }; | ||
assert_eq!(actual, expected); | ||
} | ||
|
||
#[test] | ||
fn duration_into_std() { | ||
let actual: StdDuration = Duration { nanos: 100_000_000 }.into(); | ||
let expected = StdDuration::from_nanos(100_000_000); | ||
assert_eq!(actual, expected); | ||
} | ||
} | ||
|
||
#[cfg(feature = "chrono")] | ||
#[cfg(test)] | ||
mod chrono_test { | ||
use super::*; | ||
|
||
#[test] | ||
fn duration_to_timedelta() { | ||
let duration = Duration::new(1_000_000_000); | ||
let chrono_duration: chrono::TimeDelta = duration.try_into().unwrap(); | ||
assert_eq!(chrono_duration.num_nanoseconds().unwrap(), 1_000_000_000); | ||
} | ||
|
||
#[test] | ||
fn timedelta_to_duration() { | ||
let chrono_duration = chrono::TimeDelta::nanoseconds(1_000_000_000); | ||
let duration: Duration = chrono_duration.try_into().unwrap(); | ||
assert_eq!(duration.nanos, 1_000_000_000); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
pub mod duration; | ||
pub mod instant; | ||
|
||
use crux_core::capability::Operation; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
use duration::Duration; | ||
use instant::Instant; | ||
|
||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] | ||
#[serde(rename_all = "camelCase")] | ||
pub enum TimeRequest { | ||
Now, | ||
NotifyAt { id: TimerId, instant: Instant }, | ||
NotifyAfter { id: TimerId, duration: Duration }, | ||
Clear { id: TimerId }, | ||
} | ||
|
||
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] | ||
pub struct TimerId(pub usize); | ||
|
||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] | ||
#[serde(rename_all = "camelCase")] | ||
pub enum TimeResponse { | ||
Now { instant: Instant }, | ||
InstantArrived { id: TimerId }, | ||
DurationElapsed { id: TimerId }, | ||
Cleared { id: TimerId }, | ||
} | ||
|
||
impl Operation for TimeRequest { | ||
type Output = TimeResponse; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.