reth_network/
flattened_response.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
use futures::Future;
use pin_project::pin_project;
use std::{
    pin::Pin,
    task::{Context, Poll},
};
use tokio::sync::oneshot::{error::RecvError, Receiver};

/// Flatten a [Receiver] message in order to get rid of the [RecvError] result
#[derive(Debug)]
#[pin_project]
pub struct FlattenedResponse<T> {
    #[pin]
    receiver: Receiver<T>,
}

impl<T, E> Future for FlattenedResponse<Result<T, E>>
where
    E: From<RecvError>,
{
    type Output = Result<T, E>;

    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        let this = self.project();

        this.receiver.poll(cx).map(|r| r.unwrap_or_else(|err| Err(err.into())))
    }
}

impl<T> From<Receiver<T>> for FlattenedResponse<T> {
    fn from(value: Receiver<T>) -> Self {
        Self { receiver: value }
    }
}