libp2p_pubsub_core/upgrade/
simple.rs1use std::convert::Infallible;
2use std::iter;
3
4use futures::future;
5use libp2p::core::{InboundUpgrade, OutboundUpgrade, UpgradeInfo};
6use libp2p::swarm::Stream;
7
8use super::upgrade_trait::ProtocolUpgradeOutput;
9
10#[derive(Debug, Clone)]
13pub struct SimpleProtocolUpgrade<TInfo> {
14 protocol_info: TInfo,
15}
16
17impl<TInfo> SimpleProtocolUpgrade<TInfo>
18where
19 TInfo: AsRef<str> + Clone + Send + 'static,
20{
21 pub fn new(protocol_info: TInfo) -> Self {
22 Self { protocol_info }
23 }
24}
25
26impl<TInfo> UpgradeInfo for SimpleProtocolUpgrade<TInfo>
27where
28 TInfo: AsRef<str> + Clone + Send + 'static,
29{
30 type Info = TInfo;
31 type InfoIter = iter::Once<Self::Info>;
32
33 fn protocol_info(&self) -> Self::InfoIter {
34 iter::once(self.protocol_info.clone())
35 }
36}
37
38impl<TInfo> InboundUpgrade<Stream> for SimpleProtocolUpgrade<TInfo>
39where
40 TInfo: AsRef<str> + Clone + Send + 'static,
41{
42 type Output = ProtocolUpgradeOutput<TInfo>;
43 type Error = Infallible;
44 type Future = future::Ready<Result<Self::Output, Self::Error>>;
45
46 fn upgrade_inbound(self, socket: Stream, info: Self::Info) -> Self::Future {
47 future::ok(ProtocolUpgradeOutput { socket, info })
48 }
49}
50
51impl<TInfo> OutboundUpgrade<Stream> for SimpleProtocolUpgrade<TInfo>
52where
53 TInfo: AsRef<str> + Clone + Send + 'static,
54{
55 type Output = ProtocolUpgradeOutput<TInfo>;
56 type Error = Infallible;
57 type Future = future::Ready<Result<Self::Output, Self::Error>>;
58
59 fn upgrade_outbound(self, socket: Stream, info: Self::Info) -> Self::Future {
60 future::ok(ProtocolUpgradeOutput { socket, info })
61 }
62}