tor_rtcompat/
unimpl.rs

1//! Implementations for unsupported stream and listener types.
2//!
3//! Sometimes we find it convenient to have an implementation for `NetStreamProvider` on an
4//! uninhabited type.  When we do, this module provides the associated types for its listener and streams.
5
6use std::io::Result as IoResult;
7use std::marker::PhantomData;
8use std::pin::Pin;
9use std::task::{Context, Poll};
10
11use crate::StreamOps;
12
13/// An unconstructable AsyncRead+AsyncWrite type.
14///
15/// (This is the type of a Stream for any unsupported address type.)
16#[derive(Debug, Clone)]
17pub struct FakeStream(void::Void);
18
19/// An unconstructable listener type.
20///
21/// (This is the type of a NetStreamListener for any unsupported address type.)
22#[derive(Debug, Clone)]
23pub struct FakeListener<ADDR>(void::Void, PhantomData<ADDR>);
24
25/// An unconstructable stream::Stream type.
26///
27/// (This is the type of a incoming connection stream for any unsupported address type.)
28pub struct FakeIncomingStreams<ADDR>(void::Void, PhantomData<ADDR>);
29
30impl futures::io::AsyncRead for FakeStream {
31    fn poll_read(
32        self: Pin<&mut Self>,
33        _cx: &mut Context<'_>,
34        _buf: &mut [u8],
35    ) -> Poll<IoResult<usize>> {
36        void::unreachable(self.0)
37    }
38}
39
40impl futures::io::AsyncWrite for FakeStream {
41    fn poll_write(
42        self: Pin<&mut Self>,
43        _cx: &mut Context<'_>,
44        _buf: &[u8],
45    ) -> Poll<IoResult<usize>> {
46        void::unreachable(self.0)
47    }
48
49    fn poll_flush(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<IoResult<()>> {
50        void::unreachable(self.0)
51    }
52
53    fn poll_close(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<IoResult<()>> {
54        void::unreachable(self.0)
55    }
56}
57
58impl<ADDR> futures::stream::Stream for FakeIncomingStreams<ADDR> {
59    type Item = IoResult<(FakeStream, ADDR)>;
60
61    fn poll_next(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
62        void::unreachable(self.0)
63    }
64}
65
66impl<ADDR> crate::traits::NetStreamListener<ADDR> for FakeListener<ADDR>
67where
68    ADDR: Unpin + Send + Sync + 'static,
69{
70    type Incoming = FakeIncomingStreams<ADDR>;
71    type Stream = FakeStream;
72    fn incoming(self) -> Self::Incoming {
73        void::unreachable(self.0)
74    }
75    fn local_addr(&self) -> IoResult<ADDR> {
76        void::unreachable(self.0)
77    }
78}
79
80impl StreamOps for FakeStream {
81    fn set_tcp_notsent_lowat(&self, _notsent_lowat: u32) -> IoResult<()> {
82        void::unreachable(self.0)
83    }
84    fn new_handle(&self) -> Box<dyn StreamOps + Send + Unpin> {
85        void::unreachable(self.0)
86    }
87}