libp2p_core/upgrade/
ready.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 directly yields the substream.
28#[derive(Debug, Copy, Clone)]
29pub struct ReadyUpgrade<P> {
30    protocol_name: P,
31}
32
33impl<P> ReadyUpgrade<P> {
34    pub fn new(protocol_name: P) -> Self {
35        Self { protocol_name }
36    }
37}
38
39impl<P> UpgradeInfo for ReadyUpgrade<P>
40where
41    P: AsRef<str> + Clone,
42{
43    type Info = P;
44    type InfoIter = iter::Once<P>;
45
46    fn protocol_info(&self) -> Self::InfoIter {
47        iter::once(self.protocol_name.clone())
48    }
49}
50
51impl<C, P> InboundUpgrade<C> for ReadyUpgrade<P>
52where
53    P: AsRef<str> + Clone,
54{
55    type Output = C;
56    type Error = Void;
57    type Future = future::Ready<Result<Self::Output, Self::Error>>;
58
59    fn upgrade_inbound(self, stream: C, _: Self::Info) -> Self::Future {
60        future::ready(Ok(stream))
61    }
62}
63
64impl<C, P> OutboundUpgrade<C> for ReadyUpgrade<P>
65where
66    P: AsRef<str> + Clone,
67{
68    type Output = C;
69    type Error = Void;
70    type Future = future::Ready<Result<Self::Output, Self::Error>>;
71
72    fn upgrade_outbound(self, stream: C, _: Self::Info) -> Self::Future {
73        future::ready(Ok(stream))
74    }
75}