Skip to content

Commit 0d48c64

Browse files
committed
std: move time implementations to sys (Hermit)
Last on the list: Hermit. Since I anticipate that Hermit will share the UNIX implementation in the future, I've left `Timespec` in the PAL to maintain a similar structure. Also, I noticed that some public `Instant` methods were redefined on the internal `Instant`. But the networking code can just use the public `Instant`, so I've removed them.
1 parent c160ce4 commit 0d48c64

3 files changed

Lines changed: 75 additions & 145 deletions

File tree

library/std/src/sys/net/connection/socket/hermit.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,9 @@ use crate::io::{self, BorrowedBuf, BorrowedCursor, IoSlice, IoSliceMut};
99
use crate::net::{Shutdown, SocketAddr};
1010
use crate::os::hermit::io::{AsFd, AsRawFd, BorrowedFd, FromRawFd, RawFd};
1111
use crate::sys::fd::FileDesc;
12-
use crate::sys::time::Instant;
1312
use crate::sys::{AsInner, FromInner, IntoInner};
1413
pub use crate::sys::{cvt, cvt_r};
15-
use crate::time::Duration;
14+
use crate::time::{Duration, Instant};
1615
use crate::{cmp, mem};
1716

1817
#[expect(non_camel_case_types)]
Lines changed: 11 additions & 143 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,32 @@
1-
#![allow(dead_code)]
1+
use hermit_abi::{self, timespec};
22

3-
use core::hash::{Hash, Hasher};
4-
5-
use super::hermit_abi::{self, CLOCK_MONOTONIC, CLOCK_REALTIME, timespec};
63
use crate::cmp::Ordering;
7-
use crate::ops::{Add, AddAssign, Sub, SubAssign};
4+
use crate::hash::{Hash, Hasher};
85
use crate::time::Duration;
96

107
const NSEC_PER_SEC: i32 = 1_000_000_000;
118

129
#[derive(Copy, Clone, Debug)]
13-
struct Timespec {
14-
t: timespec,
10+
pub struct Timespec {
11+
pub t: timespec,
1512
}
1613

1714
impl Timespec {
18-
const MAX: Timespec = Self::new(i64::MAX, 1_000_000_000 - 1);
15+
pub const MAX: Timespec = Self::new(i64::MAX, 1_000_000_000 - 1);
1916

20-
const MIN: Timespec = Self::new(i64::MIN, 0);
17+
pub const MIN: Timespec = Self::new(i64::MIN, 0);
2118

22-
const fn zero() -> Timespec {
19+
pub const fn zero() -> Timespec {
2320
Timespec { t: timespec { tv_sec: 0, tv_nsec: 0 } }
2421
}
2522

26-
const fn new(tv_sec: i64, tv_nsec: i32) -> Timespec {
23+
pub const fn new(tv_sec: i64, tv_nsec: i32) -> Timespec {
2724
assert!(tv_nsec >= 0 && tv_nsec < NSEC_PER_SEC);
2825
// SAFETY: The assert above checks tv_nsec is within the valid range
2926
Timespec { t: timespec { tv_sec, tv_nsec } }
3027
}
3128

32-
fn sub_timespec(&self, other: &Timespec) -> Result<Duration, Duration> {
29+
pub fn sub_timespec(&self, other: &Timespec) -> Result<Duration, Duration> {
3330
fn sub_ge_to_unsigned(a: i64, b: i64) -> u64 {
3431
debug_assert!(a >= b);
3532
a.wrapping_sub(b).cast_unsigned()
@@ -57,7 +54,7 @@ impl Timespec {
5754
}
5855
}
5956

60-
fn checked_add_duration(&self, other: &Duration) -> Option<Timespec> {
57+
pub fn checked_add_duration(&self, other: &Duration) -> Option<Timespec> {
6158
let mut secs = self.t.tv_sec.checked_add_unsigned(other.as_secs())?;
6259

6360
// Nano calculations can't overflow because nanos are <1B which fit
@@ -70,7 +67,7 @@ impl Timespec {
7067
Some(Timespec { t: timespec { tv_sec: secs, tv_nsec: nsec as _ } })
7168
}
7269

73-
fn checked_sub_duration(&self, other: &Duration) -> Option<Timespec> {
70+
pub fn checked_sub_duration(&self, other: &Duration) -> Option<Timespec> {
7471
let mut secs = self.t.tv_sec.checked_sub_unsigned(other.as_secs())?;
7572

7673
// Similar to above, nanos can't overflow.
@@ -111,132 +108,3 @@ impl Hash for Timespec {
111108
self.t.tv_nsec.hash(state);
112109
}
113110
}
114-
115-
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
116-
pub struct Instant(Timespec);
117-
118-
impl Instant {
119-
pub fn now() -> Instant {
120-
let mut time: Timespec = Timespec::zero();
121-
let _ = unsafe { hermit_abi::clock_gettime(CLOCK_MONOTONIC, &raw mut time.t) };
122-
123-
Instant(time)
124-
}
125-
126-
#[stable(feature = "time2", since = "1.8.0")]
127-
pub fn elapsed(&self) -> Duration {
128-
Instant::now() - *self
129-
}
130-
131-
pub fn duration_since(&self, earlier: Instant) -> Duration {
132-
self.checked_duration_since(earlier).unwrap_or_default()
133-
}
134-
135-
pub fn checked_duration_since(&self, earlier: Instant) -> Option<Duration> {
136-
self.checked_sub_instant(&earlier)
137-
}
138-
139-
pub fn checked_sub_instant(&self, other: &Instant) -> Option<Duration> {
140-
self.0.sub_timespec(&other.0).ok()
141-
}
142-
143-
pub fn checked_add_duration(&self, other: &Duration) -> Option<Instant> {
144-
Some(Instant(self.0.checked_add_duration(other)?))
145-
}
146-
147-
pub fn checked_sub_duration(&self, other: &Duration) -> Option<Instant> {
148-
Some(Instant(self.0.checked_sub_duration(other)?))
149-
}
150-
151-
pub fn checked_add(&self, duration: Duration) -> Option<Instant> {
152-
self.0.checked_add_duration(&duration).map(Instant)
153-
}
154-
155-
pub fn checked_sub(&self, duration: Duration) -> Option<Instant> {
156-
self.0.checked_sub_duration(&duration).map(Instant)
157-
}
158-
}
159-
160-
impl Add<Duration> for Instant {
161-
type Output = Instant;
162-
163-
/// # Panics
164-
///
165-
/// This function may panic if the resulting point in time cannot be represented by the
166-
/// underlying data structure. See [`Instant::checked_add`] for a version without panic.
167-
fn add(self, other: Duration) -> Instant {
168-
self.checked_add(other).expect("overflow when adding duration to instant")
169-
}
170-
}
171-
172-
impl AddAssign<Duration> for Instant {
173-
fn add_assign(&mut self, other: Duration) {
174-
*self = *self + other;
175-
}
176-
}
177-
178-
impl Sub<Duration> for Instant {
179-
type Output = Instant;
180-
181-
fn sub(self, other: Duration) -> Instant {
182-
self.checked_sub(other).expect("overflow when subtracting duration from instant")
183-
}
184-
}
185-
186-
impl SubAssign<Duration> for Instant {
187-
fn sub_assign(&mut self, other: Duration) {
188-
*self = *self - other;
189-
}
190-
}
191-
192-
impl Sub<Instant> for Instant {
193-
type Output = Duration;
194-
195-
/// Returns the amount of time elapsed from another instant to this one,
196-
/// or zero duration if that instant is later than this one.
197-
///
198-
/// # Panics
199-
///
200-
/// Previous Rust versions panicked when `other` was later than `self`. Currently this
201-
/// method saturates. Future versions may reintroduce the panic in some circumstances.
202-
/// See [Monotonicity].
203-
///
204-
/// [Monotonicity]: Instant#monotonicity
205-
fn sub(self, other: Instant) -> Duration {
206-
self.duration_since(other)
207-
}
208-
}
209-
210-
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
211-
pub struct SystemTime(Timespec);
212-
213-
pub const UNIX_EPOCH: SystemTime = SystemTime(Timespec::zero());
214-
215-
impl SystemTime {
216-
pub const MAX: SystemTime = SystemTime(Timespec::MAX);
217-
218-
pub const MIN: SystemTime = SystemTime(Timespec::MIN);
219-
220-
pub fn new(tv_sec: i64, tv_nsec: i32) -> SystemTime {
221-
SystemTime(Timespec::new(tv_sec, tv_nsec))
222-
}
223-
224-
pub fn now() -> SystemTime {
225-
let mut time: Timespec = Timespec::zero();
226-
let _ = unsafe { hermit_abi::clock_gettime(CLOCK_REALTIME, &raw mut time.t) };
227-
228-
SystemTime(time)
229-
}
230-
231-
pub fn sub_time(&self, other: &SystemTime) -> Result<Duration, Duration> {
232-
self.0.sub_timespec(&other.0)
233-
}
234-
235-
pub fn checked_add_duration(&self, other: &Duration) -> Option<SystemTime> {
236-
Some(SystemTime(self.0.checked_add_duration(other)?))
237-
}
238-
239-
pub fn checked_sub_duration(&self, other: &Duration) -> Option<SystemTime> {
240-
Some(SystemTime(self.0.checked_sub_duration(other)?))
241-
}
242-
}

library/std/src/sys/time/hermit.rs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
use hermit_abi::{self, CLOCK_MONOTONIC, CLOCK_REALTIME};
2+
3+
use crate::hash::Hash;
4+
use crate::sys::pal::time::Timespec;
5+
use crate::time::Duration;
6+
7+
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
8+
pub struct Instant(Timespec);
9+
10+
impl Instant {
11+
pub fn now() -> Instant {
12+
let mut time: Timespec = Timespec::zero();
13+
let _ = unsafe { hermit_abi::clock_gettime(CLOCK_MONOTONIC, &raw mut time.t) };
14+
15+
Instant(time)
16+
}
17+
18+
pub fn checked_sub_instant(&self, other: &Instant) -> Option<Duration> {
19+
self.0.sub_timespec(&other.0).ok()
20+
}
21+
22+
pub fn checked_add_duration(&self, other: &Duration) -> Option<Instant> {
23+
Some(Instant(self.0.checked_add_duration(other)?))
24+
}
25+
26+
pub fn checked_sub_duration(&self, other: &Duration) -> Option<Instant> {
27+
Some(Instant(self.0.checked_sub_duration(other)?))
28+
}
29+
}
30+
31+
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
32+
pub struct SystemTime(Timespec);
33+
34+
pub const UNIX_EPOCH: SystemTime = SystemTime(Timespec::zero());
35+
36+
impl SystemTime {
37+
pub const MAX: SystemTime = SystemTime(Timespec::MAX);
38+
39+
pub const MIN: SystemTime = SystemTime(Timespec::MIN);
40+
41+
pub fn new(tv_sec: i64, tv_nsec: i32) -> SystemTime {
42+
SystemTime(Timespec::new(tv_sec, tv_nsec))
43+
}
44+
45+
pub fn now() -> SystemTime {
46+
let mut time: Timespec = Timespec::zero();
47+
let _ = unsafe { hermit_abi::clock_gettime(CLOCK_REALTIME, &raw mut time.t) };
48+
49+
SystemTime(time)
50+
}
51+
52+
pub fn sub_time(&self, other: &SystemTime) -> Result<Duration, Duration> {
53+
self.0.sub_timespec(&other.0)
54+
}
55+
56+
pub fn checked_add_duration(&self, other: &Duration) -> Option<SystemTime> {
57+
Some(SystemTime(self.0.checked_add_duration(other)?))
58+
}
59+
60+
pub fn checked_sub_duration(&self, other: &Duration) -> Option<SystemTime> {
61+
Some(SystemTime(self.0.checked_sub_duration(other)?))
62+
}
63+
}

0 commit comments

Comments
 (0)