libp2p_core/upgrade/
pending.rs

1// Copyright 2022 Protocol Labs.
2// Copyright 2017-2018 Parity Technologies (UK) Ltd.
3//
4// Permission is hereby granted, free of charge, to any person obtaining a
5// copy of this software and associated documentation files (the "Software"),
6// to deal in the Software without restriction, including without limitation
7// the rights to use, copy, modify, merge, publish, distribute, sublicense,
8// and/or sell copies of the Software, and to permit persons to whom the
9// Software is furnished to do so, subject to the following conditions:
10//
11// The above copyright notice and this permission notice shall be included in
12// all copies or substantial portions of the Software.
13//
14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20// DEALINGS IN THE SOFTWARE.
21
22use std::{convert::Infallible, iter};
23
24use futures::future;
25
26use crate::upgrade::{InboundUpgrade, OutboundUpgrade, UpgradeInfo};
27
28/// Implementation of [`UpgradeInfo`], [`InboundUpgrade`] and [`OutboundUpgrade`] that always
29/// returns a pending upgrade.
30#[derive(Debug, Copy, Clone)]
31pub struct PendingUpgrade<P> {
32    protocol_name: P,
33}
34
35impl<P> PendingUpgrade<P> {
36    pub fn new(protocol_name: P) -> Self {
37        Self { protocol_name }
38    }
39}
40
41impl<P> UpgradeInfo for PendingUpgrade<P>
42where
43    P: AsRef<str> + Clone,
44{
45    type Info = P;
46    type InfoIter = iter::Once<P>;
47
48    fn protocol_info(&self) -> Self::InfoIter {
49        iter::once(self.protocol_name.clone())
50    }
51}
52
53impl<C, P> InboundUpgrade<C> for PendingUpgrade<P>
54where
55    P: AsRef<str> + Clone,
56{
57    type Output = Infallible;
58    type Error = Infallible;
59    type Future = future::Pending<Result<Self::Output, Self::Error>>;
60
61    fn upgrade_inbound(self, _: C, _: Self::Info) -> Self::Future {
62        future::pending()
63    }
64}
65
66impl<C, P> OutboundUpgrade<C> for PendingUpgrade<P>
67where
68    P: AsRef<str> + Clone,
69{
70    type Output = Infallible;
71    type Error = Infallible;
72    type Future = future::Pending<Result<Self::Output, Self::Error>>;
73
74    fn upgrade_outbound(self, _: C, _: Self::Info) -> Self::Future {
75        future::pending()
76    }
77}