Skip to content

Commit

Permalink
even more code libp2p#2831
Browse files Browse the repository at this point in the history
  • Loading branch information
efarg committed Nov 23, 2022
1 parent 15de17a commit 93cd417
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 26 deletions.
11 changes: 5 additions & 6 deletions swarm/src/handler/multi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -397,9 +397,9 @@ where

/// Index and protocol name pair used as `UpgradeInfo::Info`.
#[derive(Debug, Clone)]
pub struct IndexedProtoName<H>(usize, H);
pub struct IndexedProtoName(usize, ProtocolName);

impl<H: ProtocolName> ProtocolName for IndexedProtoName<H> {
impl IndexedProtoName {
fn protocol_name(&self) -> &[u8] {
self.1.protocol_name()
}
Expand Down Expand Up @@ -455,8 +455,7 @@ where
H: UpgradeInfoSend,
K: Send + 'static,
{
type Info = IndexedProtoName<H::Info>;
type InfoIter = std::vec::IntoIter<Self::Info>;
type InfoIter = std::vec::IntoIter<IndexedProtoName>;

fn protocol_info(&self) -> Self::InfoIter {
self.upgrades
Expand All @@ -478,7 +477,7 @@ where
type Error = (K, <H as InboundUpgradeSend>::Error);
type Future = BoxFuture<'static, Result<Self::Output, Self::Error>>;

fn upgrade_inbound(mut self, resource: NegotiatedSubstream, info: Self::Info) -> Self::Future {
fn upgrade_inbound(mut self, resource: NegotiatedSubstream, info: IndexedProtoName) -> Self::Future {
let IndexedProtoName(index, info) = info;
let (key, upgrade) = self.upgrades.remove(index);
upgrade
Expand All @@ -500,7 +499,7 @@ where
type Error = (K, <H as OutboundUpgradeSend>::Error);
type Future = BoxFuture<'static, Result<Self::Output, Self::Error>>;

fn upgrade_outbound(mut self, resource: NegotiatedSubstream, info: Self::Info) -> Self::Future {
fn upgrade_outbound(mut self, resource: NegotiatedSubstream, info: IndexedProtoName) -> Self::Future {
let IndexedProtoName(index, info) = info;
let (key, upgrade) = self.upgrades.remove(index);
upgrade
Expand Down
4 changes: 2 additions & 2 deletions swarm/src/handler/pending.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ impl ConnectionHandler for PendingConnectionHandler {
type InEvent = Void;
type OutEvent = Void;
type Error = Void;
type InboundProtocol = PendingUpgrade<String>;
type OutboundProtocol = PendingUpgrade<String>;
type InboundProtocol = PendingUpgrade;
type OutboundProtocol = PendingUpgrade;
type OutboundOpenInfo = Void;
type InboundOpenInfo = ();

Expand Down
29 changes: 11 additions & 18 deletions swarm/src/upgrade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,8 @@ use libp2p_core::upgrade;
/// Do not implement this trait yourself. Instead, please implement
/// [`UpgradeInfo`](upgrade::UpgradeInfo).
pub trait UpgradeInfoSend: Send + 'static {
/// Equivalent to [`UpgradeInfo::Info`](upgrade::UpgradeInfo::Info).
type Info: upgrade::ProtocolName + Clone + Send + 'static;
/// Equivalent to [`UpgradeInfo::InfoIter`](upgrade::UpgradeInfo::InfoIter).
type InfoIter: Iterator<Item = Self::Info> + Send + 'static;
type InfoIter;

/// Equivalent to [`UpgradeInfo::protocol_info`](upgrade::UpgradeInfo::protocol_info).
fn protocol_info(&self) -> Self::InfoIter;
Expand All @@ -41,10 +39,8 @@ pub trait UpgradeInfoSend: Send + 'static {
impl<T> UpgradeInfoSend for T
where
T: upgrade::UpgradeInfo + Send + 'static,
T::Info: Send + 'static,
<T::InfoIter as IntoIterator>::IntoIter: Send + 'static,
{
type Info = T::Info;
type InfoIter = <T::InfoIter as IntoIterator>::IntoIter;

fn protocol_info(&self) -> Self::InfoIter {
Expand All @@ -66,13 +62,12 @@ pub trait OutboundUpgradeSend: UpgradeInfoSend {
type Future: Future<Output = Result<Self::Output, Self::Error>> + Send + 'static;

/// Equivalent to [`OutboundUpgrade::upgrade_outbound`](upgrade::OutboundUpgrade::upgrade_outbound).
fn upgrade_outbound(self, socket: NegotiatedSubstream, info: Self::Info) -> Self::Future;
fn upgrade_outbound(self, socket: NegotiatedSubstream, info: upgrade::ProtocolName) -> Self::Future;
}

impl<T, TInfo> OutboundUpgradeSend for T
impl<T> OutboundUpgradeSend for T
where
T: upgrade::OutboundUpgrade<NegotiatedSubstream, Info = TInfo> + UpgradeInfoSend<Info = TInfo>,
TInfo: upgrade::ProtocolName + Clone + Send + 'static,
T: upgrade::OutboundUpgrade<NegotiatedSubstream> + UpgradeInfoSend,
T::Output: Send + 'static,
T::Error: Send + 'static,
T::Future: Send + 'static,
Expand All @@ -81,7 +76,7 @@ where
type Error = T::Error;
type Future = T::Future;

fn upgrade_outbound(self, socket: NegotiatedSubstream, info: TInfo) -> Self::Future {
fn upgrade_outbound(self, socket: NegotiatedSubstream, info: upgrade::ProtocolName) -> Self::Future {
upgrade::OutboundUpgrade::upgrade_outbound(self, socket, info)
}
}
Expand All @@ -100,13 +95,12 @@ pub trait InboundUpgradeSend: UpgradeInfoSend {
type Future: Future<Output = Result<Self::Output, Self::Error>> + Send + 'static;

/// Equivalent to [`InboundUpgrade::upgrade_inbound`](upgrade::InboundUpgrade::upgrade_inbound).
fn upgrade_inbound(self, socket: NegotiatedSubstream, info: Self::Info) -> Self::Future;
fn upgrade_inbound(self, socket: NegotiatedSubstream, info: upgrade::ProtocolName) -> Self::Future;
}

impl<T, TInfo> InboundUpgradeSend for T
impl<T> InboundUpgradeSend for T
where
T: upgrade::InboundUpgrade<NegotiatedSubstream, Info = TInfo> + UpgradeInfoSend<Info = TInfo>,
TInfo: upgrade::ProtocolName + Clone + Send + 'static,
T: upgrade::InboundUpgrade<NegotiatedSubstream> + UpgradeInfoSend,
T::Output: Send + 'static,
T::Error: Send + 'static,
T::Future: Send + 'static,
Expand All @@ -115,7 +109,7 @@ where
type Error = T::Error;
type Future = T::Future;

fn upgrade_inbound(self, socket: NegotiatedSubstream, info: TInfo) -> Self::Future {
fn upgrade_inbound(self, socket: NegotiatedSubstream, info: upgrade::ProtocolName) -> Self::Future {
upgrade::InboundUpgrade::upgrade_inbound(self, socket, info)
}
}
Expand All @@ -129,7 +123,6 @@ where
pub struct SendWrapper<T>(pub T);

impl<T: UpgradeInfoSend> upgrade::UpgradeInfo for SendWrapper<T> {
type Info = T::Info;
type InfoIter = T::InfoIter;

fn protocol_info(&self) -> Self::InfoIter {
Expand All @@ -142,7 +135,7 @@ impl<T: OutboundUpgradeSend> upgrade::OutboundUpgrade<NegotiatedSubstream> for S
type Error = T::Error;
type Future = T::Future;

fn upgrade_outbound(self, socket: NegotiatedSubstream, info: T::Info) -> Self::Future {
fn upgrade_outbound(self, socket: NegotiatedSubstream, info: upgrade::ProtocolName) -> Self::Future {
OutboundUpgradeSend::upgrade_outbound(self.0, socket, info)
}
}
Expand All @@ -152,7 +145,7 @@ impl<T: InboundUpgradeSend> upgrade::InboundUpgrade<NegotiatedSubstream> for Sen
type Error = T::Error;
type Future = T::Future;

fn upgrade_inbound(self, socket: NegotiatedSubstream, info: T::Info) -> Self::Future {
fn upgrade_inbound(self, socket: NegotiatedSubstream, info: upgrade::ProtocolName) -> Self::Future {
InboundUpgradeSend::upgrade_inbound(self.0, socket, info)
}
}

0 comments on commit 93cd417

Please sign in to comment.