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
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
use pin_project::pin_project;
use std::{
    marker::PhantomData,
    pin::Pin,
    task::{Context, Poll},
};

use futures::{Sink, Stream};
use tokio::sync::mpsc;

#[pin_project]
pub struct ReceiverStream<T, E> {
    #[pin]
    inner: mpsc::UnboundedReceiver<T>,
    _error: PhantomData<E>,
}

pub struct SenderSink<T, E> {
    inner: mpsc::UnboundedSender<T>,
    _error: PhantomData<E>,
}

#[pin_project]
pub struct FakeSocket<T, E> {
    #[pin]
    sender: SenderSink<T, E>,
    #[pin]
    receiver: ReceiverStream<T, E>,
}

impl<T, E> ReceiverStream<T, E> {
    pub fn new(inner: mpsc::UnboundedReceiver<T>) -> Self {
        Self {
            inner,
            _error: PhantomData::default(),
        }
    }
}

impl<T, E> SenderSink<T, E> {
    pub fn new(inner: mpsc::UnboundedSender<T>) -> Self {
        Self {
            inner,
            _error: PhantomData::default(),
        }
    }
}

impl<T, E> FakeSocket<T, E> {
    pub fn new(rx: mpsc::UnboundedReceiver<T>, tx: mpsc::UnboundedSender<T>) -> Self {
        Self {
            sender: SenderSink::new(tx),
            receiver: ReceiverStream::new(rx),
        }
    }
}

impl<T, E> Stream for ReceiverStream<T, E> {
    type Item = Result<T, E>;

    fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
        let data = futures::ready!(self.inner.poll_recv(cx));
        Poll::Ready(Ok(data).transpose())
    }
}

impl<T, E> Stream for FakeSocket<T, E> {
    type Item = Result<T, E>;

    fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
        self.project().receiver.poll_next(cx)
    }
}

impl<T, E> Sink<T> for SenderSink<T, E> {
    type Error = E;

    fn poll_ready(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
        Poll::Ready(Ok(()))
    }

    fn start_send(self: Pin<&mut Self>, item: T) -> Result<(), Self::Error> {
        let _ = self.inner.send(item);
        Ok(())
    }

    fn poll_flush(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
        Poll::Ready(Ok(()))
    }

    fn poll_close(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
        Poll::Ready(Ok(()))
    }
}

impl<T, E> Sink<T> for FakeSocket<T, E> {
    type Error = E;

    fn poll_ready(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
        self.project().sender.poll_ready(cx)
    }

    fn start_send(self: Pin<&mut Self>, item: T) -> Result<(), Self::Error> {
        self.project().sender.start_send(item)
    }

    fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
        self.project().sender.poll_flush(cx)
    }

    fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
        self.project().sender.poll_close(cx)
    }
}