Skip to content

Commit fe571d0

Browse files
committed
replace std::time with web_time
1 parent a973363 commit fe571d0

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ url = "2.5"
2323
futures = "0.3"
2424
tokio = { version = "1.43", default-features = false, features = ["sync", "time"] }
2525
serde_path_to_error = "0.1"
26+
web-time = { version = "1.1" }
2627

2728
[dev-dependencies]
2829
tokio = { version = "1.43", features = ["macros"]}

src/async_client.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ use serde::de::DeserializeOwned;
66

77
use std::collections::VecDeque;
88

9+
use web_time::Duration;
10+
911
use super::Error;
1012
use crate::error::JsonDecodeError;
1113
use crate::types::*;
@@ -15,8 +17,8 @@ use crate::util::*;
1517
#[derive(Clone)]
1618
pub struct Client {
1719
client: HttpClient,
18-
rate_limit: std::time::Duration,
19-
last_request_time: std::sync::Arc<tokio::sync::Mutex<Option<tokio::time::Instant>>>,
20+
rate_limit: Duration,
21+
last_request_time: std::sync::Arc<tokio::sync::Mutex<Option<web_time::Instant>>>,
2022
base_url: Url,
2123
}
2224

@@ -113,17 +115,18 @@ impl Client {
113115
/// Example user agent: `"my_bot (my_bot.com/info)"` or `"my_bot (help@my_bot.com)"`.
114116
///
115117
/// ```rust
118+
/// # use web_time::Duration;
116119
/// # fn f() -> Result<(), Box<dyn std::error::Error>> {
117120
/// let client = crates_io_api::AsyncClient::new(
118121
/// "my_bot (help@my_bot.com)",
119-
/// std::time::Duration::from_millis(1000),
122+
/// Duration::from_millis(1000),
120123
/// ).unwrap();
121124
/// # Ok(())
122125
/// # }
123126
/// ```
124127
pub fn new(
125128
user_agent: &str,
126-
rate_limit: std::time::Duration,
129+
rate_limit: Duration,
127130
) -> Result<Self, reqwest::header::InvalidHeaderValue> {
128131
let mut headers = header::HeaderMap::new();
129132
headers.insert(
@@ -147,7 +150,7 @@ impl Client {
147150
/// At most one request will be executed in the specified duration.
148151
/// The guidelines suggest 1 per second or less.
149152
/// (Only one request is executed concurrenly, even if the given Duration is 0).
150-
pub fn with_http_client(client: HttpClient, rate_limit: std::time::Duration) -> Self {
153+
pub fn with_http_client(client: HttpClient, rate_limit: Duration) -> Self {
151154
let limiter = std::sync::Arc::new(tokio::sync::Mutex::new(None));
152155

153156
Self {
@@ -167,7 +170,7 @@ impl Client {
167170
}
168171
}
169172

170-
let time = tokio::time::Instant::now();
173+
let time = web_time::Instant::now();
171174
let res = self.client.get(url.clone()).send().await?;
172175

173176
if !res.status().is_success() {
@@ -399,7 +402,7 @@ mod test {
399402
fn build_test_client() -> Client {
400403
Client::new(
401404
"crates-io-api-continuous-integration (github.com/theduke/crates-io-api)",
402-
std::time::Duration::from_millis(1000),
405+
web_time::Duration::from_millis(1000),
403406
)
404407
.unwrap()
405408
}

0 commit comments

Comments
 (0)