tower/limit/concurrency/
future.rs

1//! [`Future`] types
2//!
3//! [`Future`]: std::future::Future
4use pin_project_lite::pin_project;
5use std::{
6    future::Future,
7    pin::Pin,
8    task::{Context, Poll},
9};
10use tokio::sync::OwnedSemaphorePermit;
11
12pin_project! {
13    /// Future for the [`ConcurrencyLimit`] service.
14    ///
15    /// [`ConcurrencyLimit`]: crate::limit::ConcurrencyLimit
16    #[derive(Debug)]
17    pub struct ResponseFuture<T> {
18        #[pin]
19        inner: T,
20        // Keep this around so that it is dropped when the future completes
21        _permit: OwnedSemaphorePermit,
22    }
23}
24
25impl<T> ResponseFuture<T> {
26    pub(crate) fn new(inner: T, _permit: OwnedSemaphorePermit) -> ResponseFuture<T> {
27        ResponseFuture { inner, _permit }
28    }
29}
30
31impl<F, T, E> Future for ResponseFuture<F>
32where
33    F: Future<Output = Result<T, E>>,
34{
35    type Output = Result<T, E>;
36
37    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
38        self.project().inner.poll(cx)
39    }
40}