diff --git a/core/src/protocols_handler.rs b/core/src/protocols_handler.rs index 38dd2fe9..b018a6fb 100644 --- a/core/src/protocols_handler.rs +++ b/core/src/protocols_handler.rs @@ -770,7 +770,7 @@ where type InboundProtocol = upgrade::OrUpgrade< upgrade::Toggleable< - upgrade::MapUpgradeErr< + upgrade::MapInboundUpgradeErr< upgrade::MapInboundUpgrade< TProto1::InboundProtocol, fn(TProto1Out) -> EitherOutput @@ -783,7 +783,7 @@ where > >, upgrade::Toggleable< - upgrade::MapUpgradeErr< + upgrade::MapInboundUpgradeErr< upgrade::MapInboundUpgrade< TProto2::InboundProtocol, fn(TProto2Out) -> EitherOutput @@ -800,7 +800,7 @@ where type OutboundProtocol = upgrade::OrUpgrade< upgrade::Toggleable< - upgrade::MapUpgradeErr< + upgrade::MapOutboundUpgradeErr< upgrade::MapOutboundUpgrade< TProto1::OutboundProtocol, fn(TProto1Out) -> EitherOutput @@ -813,7 +813,7 @@ where > >, upgrade::Toggleable< - upgrade::MapUpgradeErr< + upgrade::MapOutboundUpgradeErr< upgrade::MapOutboundUpgrade< TProto2::OutboundProtocol, fn(TProto2Out) -> EitherOutput diff --git a/core/src/upgrade/map.rs b/core/src/upgrade/map.rs index 54dd62f3..d1cf0004 100644 --- a/core/src/upgrade/map.rs +++ b/core/src/upgrade/map.rs @@ -60,10 +60,9 @@ where } } -impl OutboundUpgrade for MapInboundUpgrade +impl OutboundUpgrade for MapInboundUpgrade where - U: OutboundUpgrade, - F: FnOnce(U::Output) -> T + U: OutboundUpgrade { type Output = U::Output; type Error = U::Error; @@ -96,10 +95,9 @@ where } } -impl InboundUpgrade for MapOutboundUpgrade +impl InboundUpgrade for MapOutboundUpgrade where - U: InboundUpgrade, - F: FnOnce(U::Output) -> T + U: InboundUpgrade { type Output = U::Output; type Error = U::Error; @@ -129,15 +127,15 @@ where /// Wraps around an upgrade and applies a closure to the error. #[derive(Debug, Clone)] -pub struct MapUpgradeErr { upgrade: U, fun: F } +pub struct MapInboundUpgradeErr { upgrade: U, fun: F } -impl MapUpgradeErr { +impl MapInboundUpgradeErr { pub fn new(upgrade: U, fun: F) -> Self { - MapUpgradeErr { upgrade, fun } + MapInboundUpgradeErr { upgrade, fun } } } -impl UpgradeInfo for MapUpgradeErr +impl UpgradeInfo for MapInboundUpgradeErr where U: UpgradeInfo { @@ -149,7 +147,7 @@ where } } -impl InboundUpgrade for MapUpgradeErr +impl InboundUpgrade for MapInboundUpgradeErr where U: InboundUpgrade, F: FnOnce(U::Error) -> T @@ -166,10 +164,45 @@ where } } -impl OutboundUpgrade for MapUpgradeErr +impl OutboundUpgrade for MapInboundUpgradeErr +where + U: OutboundUpgrade +{ + 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 error. +#[derive(Debug, Clone)] +pub struct MapOutboundUpgradeErr { upgrade: U, fun: F } + +impl MapOutboundUpgradeErr { + pub fn new(upgrade: U, fun: F) -> Self { + MapOutboundUpgradeErr { upgrade, fun } + } +} + +impl UpgradeInfo for MapOutboundUpgradeErr +where + U: UpgradeInfo +{ + type UpgradeId = U::UpgradeId; + type NamesIter = U::NamesIter; + + fn protocol_names(&self) -> Self::NamesIter { + self.upgrade.protocol_names() + } +} + +impl OutboundUpgrade for MapOutboundUpgradeErr where U: OutboundUpgrade, - F: FnOnce(U::Error) -> T, + F: FnOnce(U::Error) -> T { type Output = U::Output; type Error = T; @@ -183,6 +216,19 @@ where } } +impl InboundUpgrade for MapOutboundUpgradeErr +where + U: InboundUpgrade +{ + 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) + } +} + pub struct MapFuture { inner: TInnerFut, map: Option, diff --git a/core/src/upgrade/mod.rs b/core/src/upgrade/mod.rs index 596d3ef6..5783ec42 100644 --- a/core/src/upgrade/mod.rs +++ b/core/src/upgrade/mod.rs @@ -71,7 +71,7 @@ pub use self::{ apply::{apply, apply_inbound, apply_outbound, InboundUpgradeApply, OutboundUpgradeApply}, denied::DeniedUpgrade, error::UpgradeError, - map::{MapInboundUpgrade, MapOutboundUpgrade, MapUpgradeErr}, + map::{MapInboundUpgrade, MapOutboundUpgrade, MapInboundUpgradeErr, MapOutboundUpgradeErr}, or::OrUpgrade, toggleable::{toggleable, Toggleable} }; @@ -119,12 +119,12 @@ pub trait InboundUpgradeExt: InboundUpgrade { } /// Returns a new object that wraps around `Self` and applies a closure to the `Error`. - fn map_inbound_err(self, f: F) -> MapUpgradeErr + fn map_inbound_err(self, f: F) -> MapInboundUpgradeErr where Self: Sized, F: FnOnce(Self::Error) -> T { - MapUpgradeErr::new(self, f) + MapInboundUpgradeErr::new(self, f) } /// Returns a new object that combines `Self` and another upgrade to support both at the same @@ -169,12 +169,12 @@ pub trait OutboundUpgradeExt: OutboundUpgrade { } /// Returns a new object that wraps around `Self` and applies a closure to the `Error`. - fn map_outbound_err(self, f: F) -> MapUpgradeErr + fn map_outbound_err(self, f: F) -> MapOutboundUpgradeErr where Self: Sized, F: FnOnce(Self::Error) -> T { - MapUpgradeErr::new(self, f) + MapOutboundUpgradeErr::new(self, f) } /// Returns a new object that combines `Self` and another upgrade to support both at the same