libp2p_core/upgrade/
optional.rs

1// Copyright 2018 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::upgrade::{InboundUpgrade, OutboundUpgrade, UpgradeInfo};
22
23/// Upgrade that can be disabled at runtime.
24///
25/// Wraps around an `Option<T>` and makes it available or not depending on whether it contains or
26/// not an upgrade.
27#[derive(Debug, Clone)]
28pub struct OptionalUpgrade<T>(Option<T>);
29
30impl<T> OptionalUpgrade<T> {
31    /// Creates an enabled `OptionalUpgrade`.
32    pub fn some(inner: T) -> Self {
33        OptionalUpgrade(Some(inner))
34    }
35
36    /// Creates a disabled `OptionalUpgrade`.
37    pub fn none() -> Self {
38        OptionalUpgrade(None)
39    }
40}
41
42impl<T> UpgradeInfo for OptionalUpgrade<T>
43where
44    T: UpgradeInfo,
45{
46    type Info = T::Info;
47    type InfoIter = Iter<<T::InfoIter as IntoIterator>::IntoIter>;
48
49    fn protocol_info(&self) -> Self::InfoIter {
50        Iter(self.0.as_ref().map(|p| p.protocol_info().into_iter()))
51    }
52}
53
54impl<C, T> InboundUpgrade<C> for OptionalUpgrade<T>
55where
56    T: InboundUpgrade<C>,
57{
58    type Output = T::Output;
59    type Error = T::Error;
60    type Future = T::Future;
61
62    fn upgrade_inbound(self, sock: C, info: Self::Info) -> Self::Future {
63        if let Some(inner) = self.0 {
64            inner.upgrade_inbound(sock, info)
65        } else {
66            panic!("Bad API usage; a protocol has been negotiated while this struct contains None")
67        }
68    }
69}
70
71impl<C, T> OutboundUpgrade<C> for OptionalUpgrade<T>
72where
73    T: OutboundUpgrade<C>,
74{
75    type Output = T::Output;
76    type Error = T::Error;
77    type Future = T::Future;
78
79    fn upgrade_outbound(self, sock: C, info: Self::Info) -> Self::Future {
80        if let Some(inner) = self.0 {
81            inner.upgrade_outbound(sock, info)
82        } else {
83            panic!("Bad API usage; a protocol has been negotiated while this struct contains None")
84        }
85    }
86}
87
88/// Iterator that flattens an `Option<T>` where `T` is an iterator.
89#[derive(Debug, Clone)]
90pub struct Iter<T>(Option<T>);
91
92impl<T> Iterator for Iter<T>
93where
94    T: Iterator,
95{
96    type Item = T::Item;
97
98    fn next(&mut self) -> Option<Self::Item> {
99        if let Some(iter) = self.0.as_mut() {
100            iter.next()
101        } else {
102            None
103        }
104    }
105
106    fn size_hint(&self) -> (usize, Option<usize>) {
107        if let Some(iter) = self.0.as_ref() {
108            iter.size_hint()
109        } else {
110            (0, Some(0))
111        }
112    }
113}
114
115impl<T> ExactSizeIterator for Iter<T>
116where
117    T: ExactSizeIterator
118{
119}