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 crate::upgrade::{InboundUpgrade, OutboundUpgrade, UpgradeInfo};
23use futures::future;
24use std::iter;
25use void::Void;
26
27/// Implementation of [`UpgradeInfo`], [`InboundUpgrade`] and [`OutboundUpgrade`] that always
28/// returns a pending upgrade.
29#[derive(Debug, Copy, Clone)]
30pub struct PendingUpgrade<P> {
31 protocol_name: P,
32}
33
34impl<P> PendingUpgrade<P> {
35 pub fn new(protocol_name: P) -> Self {
36 Self { protocol_name }
37 }
38}
39
40impl<P> UpgradeInfo for PendingUpgrade<P>
41where
42 P: AsRef<str> + Clone,
43{
44 type Info = P;
45 type InfoIter = iter::Once<P>;
46
47 fn protocol_info(&self) -> Self::InfoIter {
48 iter::once(self.protocol_name.clone())
49 }
50}
51
52impl<C, P> InboundUpgrade<C> for PendingUpgrade<P>
53where
54 P: AsRef<str> + Clone,
55{
56 type Output = Void;
57 type Error = Void;
58 type Future = future::Pending<Result<Self::Output, Self::Error>>;
59
60 fn upgrade_inbound(self, _: C, _: Self::Info) -> Self::Future {
61 future::pending()
62 }
63}
64
65impl<C, P> OutboundUpgrade<C> for PendingUpgrade<P>
66where
67 P: AsRef<str> + Clone,
68{
69 type Output = Void;
70 type Error = Void;
71 type Future = future::Pending<Result<Self::Output, Self::Error>>;
72
73 fn upgrade_outbound(self, _: C, _: Self::Info) -> Self::Future {
74 future::pending()
75 }
76}