Skip to content

Commit 9b51015

Browse files
Gavin-Niedermanmax-niederman
authored andcommitted
add: Instant implementation
1 parent 31c0e19 commit 9b51015

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

library/std/src/sys/pal/vexos/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ pub mod stdio;
2020
pub mod thread;
2121
#[path = "../unsupported/thread_local_key.rs"]
2222
pub mod thread_local_key;
23-
#[path = "../unsupported/time.rs"]
2423
pub mod time;
2524

2625
use crate::{arch::asm, ptr::{self, addr_of_mut}};

library/std/src/sys/pal/vexos/time.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
use crate::time::Duration;
2+
3+
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
4+
pub struct Instant(Duration);
5+
6+
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
7+
pub struct SystemTime(Duration);
8+
9+
pub const UNIX_EPOCH: SystemTime = SystemTime(Duration::from_secs(0));
10+
11+
impl Instant {
12+
pub fn now() -> Instant {
13+
let micros = unsafe { vex_sdk::vexSystemHighResTimeGet() };
14+
Self(Duration::from_micros(micros))
15+
}
16+
17+
pub fn checked_sub_instant(&self, other: &Instant) -> Option<Duration> {
18+
self.0.checked_sub(other.0)
19+
}
20+
21+
pub fn checked_add_duration(&self, other: &Duration) -> Option<Instant> {
22+
Some(Instant(self.0.checked_add(*other)?))
23+
}
24+
25+
pub fn checked_sub_duration(&self, other: &Duration) -> Option<Instant> {
26+
Some(Instant(self.0.checked_sub(*other)?))
27+
}
28+
}
29+
30+
impl SystemTime {
31+
pub fn now() -> SystemTime {
32+
panic!("time not implemented on this platform")
33+
}
34+
35+
pub fn sub_time(&self, other: &SystemTime) -> Result<Duration, Duration> {
36+
self.0.checked_sub(other.0).ok_or_else(|| other.0 - self.0)
37+
}
38+
39+
pub fn checked_add_duration(&self, other: &Duration) -> Option<SystemTime> {
40+
Some(SystemTime(self.0.checked_add(*other)?))
41+
}
42+
43+
pub fn checked_sub_duration(&self, other: &Duration) -> Option<SystemTime> {
44+
Some(SystemTime(self.0.checked_sub(*other)?))
45+
}
46+
}

0 commit comments

Comments
 (0)