libp2p_core/transport/
dummy.rs1use crate::transport::{DialOpts, ListenerId, Transport, TransportError, TransportEvent};
22use crate::Multiaddr;
23use futures::{prelude::*, task::Context, task::Poll};
24use std::{fmt, io, marker::PhantomData, pin::Pin};
25
26pub struct DummyTransport<TOut = DummyStream>(PhantomData<TOut>);
30
31impl<TOut> DummyTransport<TOut> {
32 pub fn new() -> Self {
34 DummyTransport(PhantomData)
35 }
36}
37
38impl<TOut> Default for DummyTransport<TOut> {
39 fn default() -> Self {
40 DummyTransport::new()
41 }
42}
43
44impl<TOut> fmt::Debug for DummyTransport<TOut> {
45 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
46 write!(f, "DummyTransport")
47 }
48}
49
50impl<TOut> Clone for DummyTransport<TOut> {
51 fn clone(&self) -> Self {
52 DummyTransport(PhantomData)
53 }
54}
55
56impl<TOut> Transport for DummyTransport<TOut> {
57 type Output = TOut;
58 type Error = io::Error;
59 type ListenerUpgrade = futures::future::Pending<Result<Self::Output, io::Error>>;
60 type Dial = futures::future::Pending<Result<Self::Output, io::Error>>;
61
62 fn listen_on(
63 &mut self,
64 _id: ListenerId,
65 addr: Multiaddr,
66 ) -> Result<(), TransportError<Self::Error>> {
67 Err(TransportError::MultiaddrNotSupported(addr))
68 }
69
70 fn remove_listener(&mut self, _id: ListenerId) -> bool {
71 false
72 }
73
74 fn dial(
75 &mut self,
76 addr: Multiaddr,
77 _opts: DialOpts,
78 ) -> Result<Self::Dial, TransportError<Self::Error>> {
79 Err(TransportError::MultiaddrNotSupported(addr))
80 }
81
82 fn poll(
83 self: Pin<&mut Self>,
84 _: &mut Context<'_>,
85 ) -> Poll<TransportEvent<Self::ListenerUpgrade, Self::Error>> {
86 Poll::Pending
87 }
88}
89
90pub struct DummyStream(());
92
93impl fmt::Debug for DummyStream {
94 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
95 write!(f, "DummyStream")
96 }
97}
98
99impl AsyncRead for DummyStream {
100 fn poll_read(
101 self: Pin<&mut Self>,
102 _: &mut Context<'_>,
103 _: &mut [u8],
104 ) -> Poll<Result<usize, io::Error>> {
105 Poll::Ready(Err(io::ErrorKind::Other.into()))
106 }
107}
108
109impl AsyncWrite for DummyStream {
110 fn poll_write(
111 self: Pin<&mut Self>,
112 _: &mut Context<'_>,
113 _: &[u8],
114 ) -> Poll<Result<usize, io::Error>> {
115 Poll::Ready(Err(io::ErrorKind::Other.into()))
116 }
117
118 fn poll_flush(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<Result<(), io::Error>> {
119 Poll::Ready(Err(io::ErrorKind::Other.into()))
120 }
121
122 fn poll_close(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<Result<(), io::Error>> {
123 Poll::Ready(Err(io::ErrorKind::Other.into()))
124 }
125}