Skip to content

Commit 2a1aa8f

Browse files
committed
http client: switch from using std::time::Duration to wstd's own Duration
1 parent 080c7ca commit 2a1aa8f

File tree

1 file changed

+15
-29
lines changed

1 file changed

+15
-29
lines changed

src/http/client.rs

Lines changed: 15 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
use super::{response::IncomingBody, Body, Error, Request, Response, Result};
22
use crate::io::{self, AsyncWrite};
33
use crate::runtime::Reactor;
4-
use std::time::Duration;
5-
use wasi::clocks::monotonic_clock::Duration as WasiDuration;
4+
use crate::time::Duration;
65
use wasi::http::types::{OutgoingBody, RequestOptions as WasiRequestOptions};
76

87
/// An HTTP client.
@@ -46,18 +45,18 @@ impl Client {
4645
}
4746

4847
/// Set timeout on connecting to HTTP server
49-
pub fn set_connect_timeout(&mut self, d: Duration) {
50-
self.options_mut().connect_timeout = Some(d);
48+
pub fn set_connect_timeout(&mut self, d: impl Into<Duration>) {
49+
self.options_mut().connect_timeout = Some(d.into());
5150
}
5251

5352
/// Set timeout on recieving first byte of the Response body
54-
pub fn set_first_byte_timeout(&mut self, d: Duration) {
55-
self.options_mut().first_byte_timeout = Some(d);
53+
pub fn set_first_byte_timeout(&mut self, d: impl Into<Duration>) {
54+
self.options_mut().first_byte_timeout = Some(d.into());
5655
}
5756

5857
/// Set timeout on recieving subsequent chunks of bytes in the Response body stream
59-
pub fn set_between_bytes_timeout(&mut self, d: Duration) {
60-
self.options_mut().between_bytes_timeout = Some(d);
58+
pub fn set_between_bytes_timeout(&mut self, d: impl Into<Duration>) {
59+
self.options_mut().between_bytes_timeout = Some(d.into());
6160
}
6261

6362
fn options_mut(&mut self) -> &mut RequestOptions {
@@ -113,36 +112,23 @@ impl RequestOptions {
113112
fn to_wasi(&self) -> Result<WasiRequestOptions> {
114113
let wasi = WasiRequestOptions::new();
115114
if let Some(timeout) = self.connect_timeout {
116-
wasi.set_connect_timeout(Some(
117-
dur_to_wasi(timeout).map_err(|e| e.context("connect timeout"))?,
118-
))
119-
.map_err(|()| {
115+
wasi.set_connect_timeout(Some(*timeout)).map_err(|()| {
120116
Error::other("wasi-http implementation does not support connect timeout option")
121117
})?;
122118
}
123119
if let Some(timeout) = self.first_byte_timeout {
124-
wasi.set_first_byte_timeout(Some(
125-
dur_to_wasi(timeout).map_err(|e| e.context("first byte timeout"))?,
126-
))
127-
.map_err(|()| {
120+
wasi.set_first_byte_timeout(Some(*timeout)).map_err(|()| {
128121
Error::other("wasi-http implementation does not support first byte timeout option")
129122
})?;
130123
}
131124
if let Some(timeout) = self.between_bytes_timeout {
132-
wasi.set_between_bytes_timeout(Some(
133-
dur_to_wasi(timeout).map_err(|e| e.context("between byte timeout"))?,
134-
))
135-
.map_err(|()| {
136-
Error::other(
137-
"wasi-http implementation does not support between byte timeout option",
138-
)
139-
})?;
125+
wasi.set_between_bytes_timeout(Some(*timeout))
126+
.map_err(|()| {
127+
Error::other(
128+
"wasi-http implementation does not support between byte timeout option",
129+
)
130+
})?;
140131
}
141132
Ok(wasi)
142133
}
143134
}
144-
fn dur_to_wasi(d: Duration) -> Result<WasiDuration> {
145-
d.as_nanos()
146-
.try_into()
147-
.map_err(|_| Error::other("duration out of range supported by wasi"))
148-
}

0 commit comments

Comments
 (0)