mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-06-19 13:01:22 +00:00
Fix mapping of upgrade. (#654)
The current implementation of `MapUpgrade` implements `InboundUpgrade` and `OutboundUpgrade` and applies the transformation in both cases which means that mapping is always applied to inbound and outbound upgrades. This commit uses separate `MapInboundUpgrade` and `MapOutboundUpgrade` types which implements both traits but only map in one direction.
This commit is contained in:
@ -771,7 +771,7 @@ where
|
||||
upgrade::OrUpgrade<
|
||||
upgrade::Toggleable<
|
||||
upgrade::MapUpgradeErr<
|
||||
upgrade::MapUpgrade<
|
||||
upgrade::MapInboundUpgrade<
|
||||
TProto1::InboundProtocol,
|
||||
fn(TProto1Out) -> EitherOutput<TProto1Out, TProto2Out>
|
||||
>,
|
||||
@ -784,7 +784,7 @@ where
|
||||
>,
|
||||
upgrade::Toggleable<
|
||||
upgrade::MapUpgradeErr<
|
||||
upgrade::MapUpgrade<
|
||||
upgrade::MapInboundUpgrade<
|
||||
TProto2::InboundProtocol,
|
||||
fn(TProto2Out) -> EitherOutput<TProto1Out, TProto2Out>
|
||||
>,
|
||||
@ -801,7 +801,7 @@ where
|
||||
upgrade::OrUpgrade<
|
||||
upgrade::Toggleable<
|
||||
upgrade::MapUpgradeErr<
|
||||
upgrade::MapUpgrade<
|
||||
upgrade::MapOutboundUpgrade<
|
||||
TProto1::OutboundProtocol,
|
||||
fn(TProto1Out) -> EitherOutput<TProto1Out, TProto2Out>
|
||||
>,
|
||||
@ -814,7 +814,7 @@ where
|
||||
>,
|
||||
upgrade::Toggleable<
|
||||
upgrade::MapUpgradeErr<
|
||||
upgrade::MapUpgrade<
|
||||
upgrade::MapOutboundUpgrade<
|
||||
TProto2::OutboundProtocol,
|
||||
fn(TProto2Out) -> EitherOutput<TProto1Out, TProto2Out>
|
||||
>,
|
||||
|
@ -23,15 +23,15 @@ use crate::upgrade::{InboundUpgrade, OutboundUpgrade, UpgradeInfo};
|
||||
|
||||
/// Wraps around an upgrade and applies a closure to the output.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct MapUpgrade<U, F> { upgrade: U, fun: F }
|
||||
pub struct MapInboundUpgrade<U, F> { upgrade: U, fun: F }
|
||||
|
||||
impl<U, F> MapUpgrade<U, F> {
|
||||
impl<U, F> MapInboundUpgrade<U, F> {
|
||||
pub fn new(upgrade: U, fun: F) -> Self {
|
||||
MapUpgrade { upgrade, fun }
|
||||
MapInboundUpgrade { upgrade, fun }
|
||||
}
|
||||
}
|
||||
|
||||
impl<U, F> UpgradeInfo for MapUpgrade<U, F>
|
||||
impl<U, F> UpgradeInfo for MapInboundUpgrade<U, F>
|
||||
where
|
||||
U: UpgradeInfo
|
||||
{
|
||||
@ -43,7 +43,7 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<C, U, F, T> InboundUpgrade<C> for MapUpgrade<U, F>
|
||||
impl<C, U, F, T> InboundUpgrade<C> for MapInboundUpgrade<U, F>
|
||||
where
|
||||
U: InboundUpgrade<C>,
|
||||
F: FnOnce(U::Output) -> T
|
||||
@ -60,7 +60,57 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<C, U, F, T> OutboundUpgrade<C> for MapUpgrade<U, F>
|
||||
impl<C, U, F, T> OutboundUpgrade<C> for MapInboundUpgrade<U, F>
|
||||
where
|
||||
U: OutboundUpgrade<C>,
|
||||
F: FnOnce(U::Output) -> T
|
||||
{
|
||||
type Output = U::Output;
|
||||
type Error = U::Error;
|
||||
type Future = U::Future;
|
||||
|
||||
fn upgrade_outbound(self, sock: C, id: Self::UpgradeId) -> Self::Future {
|
||||
self.upgrade.upgrade_outbound(sock, id)
|
||||
}
|
||||
}
|
||||
|
||||
/// Wraps around an upgrade and applies a closure to the output.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct MapOutboundUpgrade<U, F> { upgrade: U, fun: F }
|
||||
|
||||
impl<U, F> MapOutboundUpgrade<U, F> {
|
||||
pub fn new(upgrade: U, fun: F) -> Self {
|
||||
MapOutboundUpgrade { upgrade, fun }
|
||||
}
|
||||
}
|
||||
|
||||
impl<U, F> UpgradeInfo for MapOutboundUpgrade<U, F>
|
||||
where
|
||||
U: UpgradeInfo
|
||||
{
|
||||
type UpgradeId = U::UpgradeId;
|
||||
type NamesIter = U::NamesIter;
|
||||
|
||||
fn protocol_names(&self) -> Self::NamesIter {
|
||||
self.upgrade.protocol_names()
|
||||
}
|
||||
}
|
||||
|
||||
impl<C, U, F, T> InboundUpgrade<C> for MapOutboundUpgrade<U, F>
|
||||
where
|
||||
U: InboundUpgrade<C>,
|
||||
F: FnOnce(U::Output) -> T
|
||||
{
|
||||
type Output = U::Output;
|
||||
type Error = U::Error;
|
||||
type Future = U::Future;
|
||||
|
||||
fn upgrade_inbound(self, sock: C, id: Self::UpgradeId) -> Self::Future {
|
||||
self.upgrade.upgrade_inbound(sock, id)
|
||||
}
|
||||
}
|
||||
|
||||
impl<C, U, F, T> OutboundUpgrade<C> for MapOutboundUpgrade<U, F>
|
||||
where
|
||||
U: OutboundUpgrade<C>,
|
||||
F: FnOnce(U::Output) -> T
|
||||
|
@ -71,7 +71,7 @@ pub use self::{
|
||||
apply::{apply_inbound, apply_outbound, InboundUpgradeApply, OutboundUpgradeApply},
|
||||
denied::DeniedUpgrade,
|
||||
error::UpgradeError,
|
||||
map::{MapUpgrade, MapUpgradeErr},
|
||||
map::{MapInboundUpgrade, MapOutboundUpgrade, MapUpgradeErr},
|
||||
or::OrUpgrade,
|
||||
toggleable::{toggleable, Toggleable}
|
||||
};
|
||||
@ -110,12 +110,12 @@ pub trait InboundUpgrade<C>: UpgradeInfo {
|
||||
/// `InboundUpgrade`.
|
||||
pub trait InboundUpgradeExt<C>: InboundUpgrade<C> {
|
||||
/// Returns a new object that wraps around `Self` and applies a closure to the `Output`.
|
||||
fn map_inbound<F, T>(self, f: F) -> MapUpgrade<Self, F>
|
||||
fn map_inbound<F, T>(self, f: F) -> MapInboundUpgrade<Self, F>
|
||||
where
|
||||
Self: Sized,
|
||||
F: FnOnce(Self::Output) -> T
|
||||
{
|
||||
MapUpgrade::new(self, f)
|
||||
MapInboundUpgrade::new(self, f)
|
||||
}
|
||||
|
||||
/// Returns a new object that wraps around `Self` and applies a closure to the `Error`.
|
||||
@ -160,12 +160,12 @@ pub trait OutboundUpgrade<C>: UpgradeInfo {
|
||||
/// `OutboundUpgrade`.
|
||||
pub trait OutboundUpgradeExt<C>: OutboundUpgrade<C> {
|
||||
/// Returns a new object that wraps around `Self` and applies a closure to the `Output`.
|
||||
fn map_outbound<F, T>(self, f: F) -> MapUpgrade<Self, F>
|
||||
fn map_outbound<F, T>(self, f: F) -> MapOutboundUpgrade<Self, F>
|
||||
where
|
||||
Self: Sized,
|
||||
F: FnOnce(Self::Output) -> T
|
||||
{
|
||||
MapUpgrade::new(self, f)
|
||||
MapOutboundUpgrade::new(self, f)
|
||||
}
|
||||
|
||||
/// Returns a new object that wraps around `Self` and applies a closure to the `Error`.
|
||||
|
Reference in New Issue
Block a user