libp2p_core/transport/optional.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 multiaddr::Multiaddr;
23use std::{pin::Pin, task::Context, task::Poll};
24
25/// Transport that is possibly disabled.
26///
27/// An `OptionalTransport<T>` is a wrapper around an `Option<T>`. If it is disabled (read: contains
28/// `None`), then any attempt to dial or listen will return `MultiaddrNotSupported`. If it is
29/// enabled (read: contains `Some`), then dialing and listening will be handled by the inner
30/// transport.
31#[derive(Debug, Copy, Clone)]
32#[pin_project::pin_project]
33pub struct OptionalTransport<T>(#[pin] Option<T>);
34
35impl<T> OptionalTransport<T> {
36 /// Builds an `OptionalTransport` with the given transport in an enabled
37 /// state.
38 pub fn some(inner: T) -> OptionalTransport<T> {
39 OptionalTransport(Some(inner))
40 }
41
42 /// Builds a disabled `OptionalTransport`.
43 pub fn none() -> OptionalTransport<T> {
44 OptionalTransport(None)
45 }
46}
47
48impl<T> From<T> for OptionalTransport<T> {
49 fn from(inner: T) -> Self {
50 OptionalTransport(Some(inner))
51 }
52}
53
54impl<T> Transport for OptionalTransport<T>
55where
56 T: Transport,
57{
58 type Output = T::Output;
59 type Error = T::Error;
60 type ListenerUpgrade = T::ListenerUpgrade;
61 type Dial = T::Dial;
62
63 fn listen_on(
64 &mut self,
65 id: ListenerId,
66 addr: Multiaddr,
67 ) -> Result<(), TransportError<Self::Error>> {
68 if let Some(inner) = self.0.as_mut() {
69 inner.listen_on(id, addr)
70 } else {
71 Err(TransportError::MultiaddrNotSupported(addr))
72 }
73 }
74
75 fn remove_listener(&mut self, id: ListenerId) -> bool {
76 if let Some(inner) = self.0.as_mut() {
77 inner.remove_listener(id)
78 } else {
79 false
80 }
81 }
82
83 fn dial(
84 &mut self,
85 addr: Multiaddr,
86 opts: DialOpts,
87 ) -> Result<Self::Dial, TransportError<Self::Error>> {
88 if let Some(inner) = self.0.as_mut() {
89 inner.dial(addr, opts)
90 } else {
91 Err(TransportError::MultiaddrNotSupported(addr))
92 }
93 }
94
95 fn poll(
96 self: Pin<&mut Self>,
97 cx: &mut Context<'_>,
98 ) -> Poll<TransportEvent<Self::ListenerUpgrade, Self::Error>> {
99 if let Some(inner) = self.project().0.as_pin_mut() {
100 inner.poll(cx)
101 } else {
102 Poll::Pending
103 }
104 }
105}