Skip to content

Commit 2894721

Browse files
authored
Use AsyncFn traits for cached client (#13530)
Make use of the 2024 edition.
1 parent 97d519c commit 2894721

File tree

1 file changed

+17
-57
lines changed

1 file changed

+17
-57
lines changed

crates/uv-client/src/cached_client.rs

Lines changed: 17 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::fmt::{Debug, Display, Formatter};
22
use std::time::{Duration, SystemTime};
3-
use std::{borrow::Cow, future::Future, path::Path};
3+
use std::{borrow::Cow, path::Path};
44

55
use futures::FutureExt;
66
use reqwest::{Request, Response};
@@ -216,19 +216,14 @@ impl CachedClient {
216216
pub async fn get_serde<
217217
Payload: Serialize + DeserializeOwned + Send + 'static,
218218
CallBackError: std::error::Error + 'static,
219-
Callback,
220-
CallbackReturn,
219+
Callback: AsyncFn(Response) -> Result<Payload, CallBackError>,
221220
>(
222221
&self,
223222
req: Request,
224223
cache_entry: &CacheEntry,
225224
cache_control: CacheControl,
226225
response_callback: Callback,
227-
) -> Result<Payload, CachedClientError<CallBackError>>
228-
where
229-
Callback: Fn(Response) -> CallbackReturn,
230-
CallbackReturn: Future<Output = Result<Payload, CallBackError>>,
231-
{
226+
) -> Result<Payload, CachedClientError<CallBackError>> {
232227
let payload = self
233228
.get_cacheable(req, cache_entry, cache_control, async |resp| {
234229
let payload = response_callback(resp).await?;
@@ -254,19 +249,14 @@ impl CachedClient {
254249
pub async fn get_cacheable<
255250
Payload: Cacheable,
256251
CallBackError: std::error::Error + 'static,
257-
Callback,
258-
CallbackReturn,
252+
Callback: AsyncFn(Response) -> Result<Payload, CallBackError>,
259253
>(
260254
&self,
261255
req: Request,
262256
cache_entry: &CacheEntry,
263257
cache_control: CacheControl,
264258
response_callback: Callback,
265-
) -> Result<Payload::Target, CachedClientError<CallBackError>>
266-
where
267-
Callback: Fn(Response) -> CallbackReturn,
268-
CallbackReturn: Future<Output = Result<Payload, CallBackError>>,
269-
{
259+
) -> Result<Payload::Target, CachedClientError<CallBackError>> {
270260
let fresh_req = req.try_clone().expect("HTTP request must be cloneable");
271261
let cached_response = if let Some(cached) = Self::read_cache(cache_entry).await {
272262
self.send_cached(req, cache_control, cached)
@@ -344,18 +334,13 @@ impl CachedClient {
344334
pub async fn skip_cache<
345335
Payload: Serialize + DeserializeOwned + Send + 'static,
346336
CallBackError: std::error::Error + 'static,
347-
Callback,
348-
CallbackReturn,
337+
Callback: AsyncFnOnce(Response) -> Result<Payload, CallBackError>,
349338
>(
350339
&self,
351340
req: Request,
352341
cache_entry: &CacheEntry,
353342
response_callback: Callback,
354-
) -> Result<Payload, CachedClientError<CallBackError>>
355-
where
356-
Callback: FnOnce(Response) -> CallbackReturn,
357-
CallbackReturn: Future<Output = Result<Payload, CallBackError>>,
358-
{
343+
) -> Result<Payload, CachedClientError<CallBackError>> {
359344
let (response, cache_policy) = self.fresh_request(req).await?;
360345

361346
let payload = self
@@ -371,18 +356,13 @@ impl CachedClient {
371356
async fn resend_and_heal_cache<
372357
Payload: Cacheable,
373358
CallBackError: std::error::Error + 'static,
374-
Callback,
375-
CallbackReturn,
359+
Callback: AsyncFnOnce(Response) -> Result<Payload, CallBackError>,
376360
>(
377361
&self,
378362
req: Request,
379363
cache_entry: &CacheEntry,
380364
response_callback: Callback,
381-
) -> Result<Payload::Target, CachedClientError<CallBackError>>
382-
where
383-
Callback: FnOnce(Response) -> CallbackReturn,
384-
CallbackReturn: Future<Output = Result<Payload, CallBackError>>,
385-
{
365+
) -> Result<Payload::Target, CachedClientError<CallBackError>> {
386366
let _ = fs_err::tokio::remove_file(&cache_entry.path()).await;
387367
let (response, cache_policy) = self.fresh_request(req).await?;
388368
self.run_response_callback(cache_entry, cache_policy, response, response_callback)
@@ -392,19 +372,14 @@ impl CachedClient {
392372
async fn run_response_callback<
393373
Payload: Cacheable,
394374
CallBackError: std::error::Error + 'static,
395-
Callback,
396-
CallbackReturn,
375+
Callback: AsyncFnOnce(Response) -> Result<Payload, CallBackError>,
397376
>(
398377
&self,
399378
cache_entry: &CacheEntry,
400379
cache_policy: Option<Box<CachePolicy>>,
401380
response: Response,
402381
response_callback: Callback,
403-
) -> Result<Payload::Target, CachedClientError<CallBackError>>
404-
where
405-
Callback: FnOnce(Response) -> CallbackReturn,
406-
CallbackReturn: Future<Output = Result<Payload, CallBackError>>,
407-
{
382+
) -> Result<Payload::Target, CachedClientError<CallBackError>> {
408383
let new_cache = info_span!("new_cache", file = %cache_entry.path().display());
409384
let data = response_callback(response)
410385
.boxed_local()
@@ -571,19 +546,14 @@ impl CachedClient {
571546
pub async fn get_serde_with_retry<
572547
Payload: Serialize + DeserializeOwned + Send + 'static,
573548
CallBackError: std::error::Error + 'static,
574-
Callback,
575-
CallbackReturn,
549+
Callback: AsyncFn(Response) -> Result<Payload, CallBackError>,
576550
>(
577551
&self,
578552
req: Request,
579553
cache_entry: &CacheEntry,
580554
cache_control: CacheControl,
581555
response_callback: Callback,
582-
) -> Result<Payload, CachedClientError<CallBackError>>
583-
where
584-
Callback: Fn(Response) -> CallbackReturn,
585-
CallbackReturn: Future<Output = Result<Payload, CallBackError>>,
586-
{
556+
) -> Result<Payload, CachedClientError<CallBackError>> {
587557
let payload = self
588558
.get_cacheable_with_retry(req, cache_entry, cache_control, async |resp| {
589559
let payload = response_callback(resp).await?;
@@ -600,19 +570,14 @@ impl CachedClient {
600570
pub async fn get_cacheable_with_retry<
601571
Payload: Cacheable,
602572
CallBackError: std::error::Error + 'static,
603-
Callback,
604-
CallbackReturn,
573+
Callback: AsyncFn(Response) -> Result<Payload, CallBackError>,
605574
>(
606575
&self,
607576
req: Request,
608577
cache_entry: &CacheEntry,
609578
cache_control: CacheControl,
610579
response_callback: Callback,
611-
) -> Result<Payload::Target, CachedClientError<CallBackError>>
612-
where
613-
Callback: Fn(Response) -> CallbackReturn,
614-
CallbackReturn: Future<Output = Result<Payload, CallBackError>>,
615-
{
580+
) -> Result<Payload::Target, CachedClientError<CallBackError>> {
616581
let mut n_past_retries = 0;
617582
let start_time = SystemTime::now();
618583
let retry_policy = self.uncached().retry_policy();
@@ -649,18 +614,13 @@ impl CachedClient {
649614
pub async fn skip_cache_with_retry<
650615
Payload: Serialize + DeserializeOwned + Send + 'static,
651616
CallBackError: std::error::Error + 'static,
652-
Callback,
653-
CallbackReturn,
617+
Callback: AsyncFn(Response) -> Result<Payload, CallBackError>,
654618
>(
655619
&self,
656620
req: Request,
657621
cache_entry: &CacheEntry,
658622
response_callback: Callback,
659-
) -> Result<Payload, CachedClientError<CallBackError>>
660-
where
661-
Callback: Fn(Response) -> CallbackReturn,
662-
CallbackReturn: Future<Output = Result<Payload, CallBackError>>,
663-
{
623+
) -> Result<Payload, CachedClientError<CallBackError>> {
664624
let mut n_past_retries = 0;
665625
let start_time = SystemTime::now();
666626
let retry_policy = self.uncached().retry_policy();

0 commit comments

Comments
 (0)