warp/filter/
unify.rs

1use std::future::Future;
2use std::pin::Pin;
3use std::task::{Context, Poll};
4
5use futures_util::{ready, TryFuture};
6use pin_project::pin_project;
7
8use super::{Either, Filter, FilterBase, Internal, Tuple};
9
10#[derive(Clone, Copy, Debug)]
11pub struct Unify<F> {
12    pub(super) filter: F,
13}
14
15impl<F, T> FilterBase for Unify<F>
16where
17    F: Filter<Extract = (Either<T, T>,)>,
18    T: Tuple,
19{
20    type Extract = T;
21    type Error = F::Error;
22    type Future = UnifyFuture<F::Future>;
23    #[inline]
24    fn filter(&self, _: Internal) -> Self::Future {
25        UnifyFuture {
26            inner: self.filter.filter(Internal),
27        }
28    }
29}
30
31#[allow(missing_debug_implementations)]
32#[pin_project]
33pub struct UnifyFuture<F> {
34    #[pin]
35    inner: F,
36}
37
38impl<F, T> Future for UnifyFuture<F>
39where
40    F: TryFuture<Ok = (Either<T, T>,)>,
41{
42    type Output = Result<T, F::Error>;
43
44    #[inline]
45    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
46        Poll::Ready(match ready!(self.project().inner.try_poll(cx))? {
47            (Either::A(x),) | (Either::B(x),) => Ok(x),
48        })
49    }
50}