libp2p_pubsub_core/upgrade/
upgrade_trait.rs

1use libp2p::core::{InboundUpgrade, OutboundUpgrade, UpgradeInfo};
2use libp2p::swarm::handler::{InboundUpgradeSend, OutboundUpgradeSend, UpgradeInfoSend};
3use libp2p::swarm::Stream;
4
5/// Output of the [`InboundUpgrade`] and [`OutboundUpgrade`] traits.
6pub struct ProtocolUpgradeOutput<TInfo> {
7    pub socket: Stream,
8    pub info: TInfo,
9}
10
11// NOTE: Use `trait_set` crate as `trait_alias` is not yet stable.
12//       https://github.com/rust-lang/rust/issues/41517
13trait_set::trait_set! {
14    pub trait ProtocolUpgradeInfo = UpgradeInfo;
15    pub trait ProtocolInboundUpgrade<TInfo> = InboundUpgrade<Stream, Output=ProtocolUpgradeOutput<TInfo>>;
16    pub trait ProtocolOutboundUpgrade<TInfo> = OutboundUpgrade<Stream, Output=ProtocolUpgradeOutput<TInfo>>;
17
18    /// The `ProtocolUpgrade` trait is an alias for the [`InboundUpgrade`], [`OutboundUpgrade`] and
19    /// [`UpgradeInfo`] traits.
20    ///
21    /// Theses traits are used by the connection handler to identify and negotiate the pubsub
22    /// protocol.
23    ///
24    /// - The [`UpgradeInfo`] trait describes the protocol name and version.
25    /// - The [`InboundUpgrade`] trait describes the protocol handshake to apply on inbound
26    ///   connections.
27    /// - The [`OutboundUpgrade`] trait describes the protocol handshake to apply on outbound
28    ///   connections.
29    pub trait ProtocolUpgrade = ProtocolUpgradeInfo
30        + ProtocolInboundUpgrade<<Self as UpgradeInfo>::Info>
31        + ProtocolOutboundUpgrade<<Self as UpgradeInfo>::Info>;
32
33
34    /// Implemented automatically on all types that implement [`ProtocolUpgrade`]
35    /// and `Send + 'static`.
36    ///
37    /// Do not implement this trait yourself. Instead, please implement
38    /// [`ProtocolUpgrade`] sub-traits.
39    pub trait ProtocolUpgradeSend = UpgradeInfoSend +
40        InboundUpgradeSend<Output=ProtocolUpgradeOutput<<Self as UpgradeInfoSend>::Info>> +
41        OutboundUpgradeSend<Output=ProtocolUpgradeOutput<<Self as UpgradeInfoSend>::Info>>;
42}