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 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
33/// Implementation of `Transport` that doesn't support any multiaddr.
34///
35/// Useful for testing purposes, or as a fallback implementation when no protocol is available.
36pub struct DummyTransport<TOut = DummyStream>(PhantomData<TOut>);
37
38impl<TOut> DummyTransport<TOut> {
39    /// Builds a new `DummyTransport`.
40    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
97/// Implementation of `AsyncRead` and `AsyncWrite`. Not meant to be instantiated.
98pub 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}