minreq/
spend.rs

1use std::{future::Future, pin::Pin, time::Duration, task::Poll, task::Context, time::Instant};
2pub struct SpendFuture<Fut: Future + Unpin> {
3    fut: Fut,
4    started_at: Option<Instant>,
5}
6
7impl<Fut: Future + Unpin> SpendFuture<Fut> {
8    pub fn new(fut: Fut, started_at: Option<Instant>) -> Self {
9        Self { fut, started_at }
10    }
11}
12
13impl<Fut: Future + Unpin> Future for SpendFuture<Fut> {
14    type Output = (Fut::Output, Duration);
15
16    fn poll(
17        self: Pin<&mut Self>,
18        cx: &mut Context<'_>,
19    ) -> Poll<Self::Output> {
20        let Self { fut, started_at } = self.get_mut();
21        let started_at = started_at.get_or_insert_with(Instant::now);
22        match Pin::new(fut).poll(cx) {
23            Poll::Pending => Poll::Pending,
24            Poll::Ready(out) => {
25                let elapsed = started_at.elapsed();
26                Poll::Ready((out, elapsed))
27            }
28        }
29    }
30}