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