diff --git a/core/src/protocols_handler.rs b/core/src/protocols_handler.rs index c5c6e0d7..38dd2fe9 100644 --- a/core/src/protocols_handler.rs +++ b/core/src/protocols_handler.rs @@ -771,7 +771,7 @@ where upgrade::OrUpgrade< upgrade::Toggleable< upgrade::MapUpgradeErr< - upgrade::MapUpgrade< + upgrade::MapInboundUpgrade< TProto1::InboundProtocol, fn(TProto1Out) -> EitherOutput >, @@ -784,7 +784,7 @@ where >, upgrade::Toggleable< upgrade::MapUpgradeErr< - upgrade::MapUpgrade< + upgrade::MapInboundUpgrade< TProto2::InboundProtocol, fn(TProto2Out) -> EitherOutput >, @@ -801,7 +801,7 @@ where upgrade::OrUpgrade< upgrade::Toggleable< upgrade::MapUpgradeErr< - upgrade::MapUpgrade< + upgrade::MapOutboundUpgrade< TProto1::OutboundProtocol, fn(TProto1Out) -> EitherOutput >, @@ -814,7 +814,7 @@ where >, upgrade::Toggleable< upgrade::MapUpgradeErr< - upgrade::MapUpgrade< + upgrade::MapOutboundUpgrade< TProto2::OutboundProtocol, fn(TProto2Out) -> EitherOutput >, diff --git a/core/src/upgrade/map.rs b/core/src/upgrade/map.rs index bac63359..54dd62f3 100644 --- a/core/src/upgrade/map.rs +++ b/core/src/upgrade/map.rs @@ -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 { upgrade: U, fun: F } +pub struct MapInboundUpgrade { upgrade: U, fun: F } -impl MapUpgrade { +impl MapInboundUpgrade { pub fn new(upgrade: U, fun: F) -> Self { - MapUpgrade { upgrade, fun } + MapInboundUpgrade { upgrade, fun } } } -impl UpgradeInfo for MapUpgrade +impl UpgradeInfo for MapInboundUpgrade where U: UpgradeInfo { @@ -43,7 +43,7 @@ where } } -impl InboundUpgrade for MapUpgrade +impl InboundUpgrade for MapInboundUpgrade where U: InboundUpgrade, F: FnOnce(U::Output) -> T @@ -60,7 +60,57 @@ where } } -impl OutboundUpgrade for MapUpgrade +impl OutboundUpgrade for MapInboundUpgrade +where + U: OutboundUpgrade, + 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 { upgrade: U, fun: F } + +impl MapOutboundUpgrade { + pub fn new(upgrade: U, fun: F) -> Self { + MapOutboundUpgrade { upgrade, fun } + } +} + +impl UpgradeInfo for MapOutboundUpgrade +where + U: UpgradeInfo +{ + type UpgradeId = U::UpgradeId; + type NamesIter = U::NamesIter; + + fn protocol_names(&self) -> Self::NamesIter { + self.upgrade.protocol_names() + } +} + +impl InboundUpgrade for MapOutboundUpgrade +where + U: InboundUpgrade, + 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 OutboundUpgrade for MapOutboundUpgrade where U: OutboundUpgrade, F: FnOnce(U::Output) -> T diff --git a/core/src/upgrade/mod.rs b/core/src/upgrade/mod.rs index 67de2a1f..59b8b5d4 100644 --- a/core/src/upgrade/mod.rs +++ b/core/src/upgrade/mod.rs @@ -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: UpgradeInfo { /// `InboundUpgrade`. pub trait InboundUpgradeExt: InboundUpgrade { /// Returns a new object that wraps around `Self` and applies a closure to the `Output`. - fn map_inbound(self, f: F) -> MapUpgrade + fn map_inbound(self, f: F) -> MapInboundUpgrade 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: UpgradeInfo { /// `OutboundUpgrade`. pub trait OutboundUpgradeExt: OutboundUpgrade { /// Returns a new object that wraps around `Self` and applies a closure to the `Output`. - fn map_outbound(self, f: F) -> MapUpgrade + fn map_outbound(self, f: F) -> MapOutboundUpgrade 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`.