Skip to content

Commit d9bad85

Browse files
committed
Use wstd APIs instead of WASI APIs.
To support this, add `Duration::as_millis` and related functions.
1 parent 77b097e commit d9bad85

File tree

3 files changed

+33
-8
lines changed

3 files changed

+33
-8
lines changed

examples/http_server.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use wstd::http::body::{BodyForthcoming, IncomingBody, OutgoingBody};
22
use wstd::http::server::{Finished, Responder};
33
use wstd::http::{IntoBody, Request, Response};
44
use wstd::io::{copy, empty, AsyncWrite};
5+
use wstd::time::{Duration, Instant};
56

67
#[wstd::http_server]
78
async fn main(request: Request<IncomingBody>, responder: Responder) -> Finished {
@@ -25,16 +26,13 @@ async fn http_home(_request: Request<IncomingBody>, responder: Responder) -> Fin
2526

2627
async fn http_wait(_request: Request<IncomingBody>, responder: Responder) -> Finished {
2728
// Get the time now
28-
let now = wasi::clocks::monotonic_clock::now();
29+
let now = Instant::now();
2930

30-
// Sleep for 1 second
31-
let nanos = 1_000_000_000;
32-
let pollable = wasi::clocks::monotonic_clock::subscribe_duration(nanos);
33-
pollable.block();
31+
// Sleep for one second.
32+
wstd::task::sleep(Duration::from_secs(1)).await;
3433

3534
// Compute how long we slept for.
36-
let elapsed = wasi::clocks::monotonic_clock::now() - now;
37-
let elapsed = elapsed / 1_000_000; // change to millis
35+
let elapsed = Instant::now().duration_since(now).as_millis();
3836

3937
// To stream data to the response body, use `Responder::start_response`.
4038
let mut body = responder.start_response(Response::new(BodyForthcoming));

src/time/duration.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,34 @@ impl Duration {
7070
pub fn from_secs_f32(secs: f32) -> Duration {
7171
std::time::Duration::from_secs_f32(secs).into()
7272
}
73+
74+
/// Returns the number of whole seconds contained by this `Duration`.
75+
#[must_use]
76+
#[inline]
77+
pub const fn as_secs(&self) -> u64 {
78+
self.0 / 1_000_000_000
79+
}
80+
81+
/// Returns the number of whole microseconds contained by this `Duration`.
82+
#[must_use]
83+
#[inline]
84+
pub const fn as_micros(&self) -> u128 {
85+
(self.0 / 1_000_000) as u128
86+
}
87+
88+
/// Returns the number of whole milliseconds contained by this `Duration`.
89+
#[must_use]
90+
#[inline]
91+
pub const fn as_millis(&self) -> u128 {
92+
(self.0 / 1_000) as u128
93+
}
94+
95+
/// Returns the total number of nanoseconds contained by this `Duration`.
96+
#[must_use]
97+
#[inline]
98+
pub const fn as_nanos(&self) -> u128 {
99+
self.0 as u128
100+
}
73101
}
74102

75103
impl std::ops::Deref for Duration {

test-programs/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,3 @@ publish = false
88
futures-lite.workspace = true
99
serde_json.workspace = true
1010
wstd.workspace = true
11-
wasi.workspace = true

0 commit comments

Comments
 (0)