libp2p_core/transport/
dummy.rs

1// Copyright 2019 Parity Technologies (UK) Ltd.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a
4// copy of this software and associated documentation files (the "Software"),
5// to deal in the Software without restriction, including without limitation
6// the rights to use, copy, modify, merge, publish, distribute, sublicense,
7// and/or sell copies of the Software, and to permit persons to whom the
8// Software is furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in
11// all copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
14// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
19// DEALINGS IN THE SOFTWARE.
20
21use 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
26/// Implementation of `Transport` that doesn't support any multiaddr.
27///
28/// Useful for testing purposes, or as a fallback implementation when no protocol is available.
29pub struct DummyTransport<TOut = DummyStream>(PhantomData<TOut>);
30
31impl<TOut> DummyTransport<TOut> {
32    /// Builds a new `DummyTransport`.
33    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
90/// Implementation of `AsyncRead` and `AsyncWrite`. Not meant to be instantiated.
91pub 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}