mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-06-03 05:01:24 +00:00
Fix mapping of upgrade errors. (#656)
The current implementation of `MapUpgradeErr` implements `InboundUpgrade` and `OutboundUpgrade` and applies the transformation in both cases which means that mapping is always applied to inbound and outbound upgrade errors. This commit uses separate `MapInboundUpgradeErr` and `MapOutboundUpgradeErr` types which implements both traits but only map errors in one direction.
This commit is contained in:
parent
e7bffd009f
commit
64af676178
@ -770,7 +770,7 @@ where
|
|||||||
type InboundProtocol =
|
type InboundProtocol =
|
||||||
upgrade::OrUpgrade<
|
upgrade::OrUpgrade<
|
||||||
upgrade::Toggleable<
|
upgrade::Toggleable<
|
||||||
upgrade::MapUpgradeErr<
|
upgrade::MapInboundUpgradeErr<
|
||||||
upgrade::MapInboundUpgrade<
|
upgrade::MapInboundUpgrade<
|
||||||
TProto1::InboundProtocol,
|
TProto1::InboundProtocol,
|
||||||
fn(TProto1Out) -> EitherOutput<TProto1Out, TProto2Out>
|
fn(TProto1Out) -> EitherOutput<TProto1Out, TProto2Out>
|
||||||
@ -783,7 +783,7 @@ where
|
|||||||
>
|
>
|
||||||
>,
|
>,
|
||||||
upgrade::Toggleable<
|
upgrade::Toggleable<
|
||||||
upgrade::MapUpgradeErr<
|
upgrade::MapInboundUpgradeErr<
|
||||||
upgrade::MapInboundUpgrade<
|
upgrade::MapInboundUpgrade<
|
||||||
TProto2::InboundProtocol,
|
TProto2::InboundProtocol,
|
||||||
fn(TProto2Out) -> EitherOutput<TProto1Out, TProto2Out>
|
fn(TProto2Out) -> EitherOutput<TProto1Out, TProto2Out>
|
||||||
@ -800,7 +800,7 @@ where
|
|||||||
type OutboundProtocol =
|
type OutboundProtocol =
|
||||||
upgrade::OrUpgrade<
|
upgrade::OrUpgrade<
|
||||||
upgrade::Toggleable<
|
upgrade::Toggleable<
|
||||||
upgrade::MapUpgradeErr<
|
upgrade::MapOutboundUpgradeErr<
|
||||||
upgrade::MapOutboundUpgrade<
|
upgrade::MapOutboundUpgrade<
|
||||||
TProto1::OutboundProtocol,
|
TProto1::OutboundProtocol,
|
||||||
fn(TProto1Out) -> EitherOutput<TProto1Out, TProto2Out>
|
fn(TProto1Out) -> EitherOutput<TProto1Out, TProto2Out>
|
||||||
@ -813,7 +813,7 @@ where
|
|||||||
>
|
>
|
||||||
>,
|
>,
|
||||||
upgrade::Toggleable<
|
upgrade::Toggleable<
|
||||||
upgrade::MapUpgradeErr<
|
upgrade::MapOutboundUpgradeErr<
|
||||||
upgrade::MapOutboundUpgrade<
|
upgrade::MapOutboundUpgrade<
|
||||||
TProto2::OutboundProtocol,
|
TProto2::OutboundProtocol,
|
||||||
fn(TProto2Out) -> EitherOutput<TProto1Out, TProto2Out>
|
fn(TProto2Out) -> EitherOutput<TProto1Out, TProto2Out>
|
||||||
|
@ -60,10 +60,9 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<C, U, F, T> OutboundUpgrade<C> for MapInboundUpgrade<U, F>
|
impl<C, U, F> OutboundUpgrade<C> for MapInboundUpgrade<U, F>
|
||||||
where
|
where
|
||||||
U: OutboundUpgrade<C>,
|
U: OutboundUpgrade<C>
|
||||||
F: FnOnce(U::Output) -> T
|
|
||||||
{
|
{
|
||||||
type Output = U::Output;
|
type Output = U::Output;
|
||||||
type Error = U::Error;
|
type Error = U::Error;
|
||||||
@ -96,10 +95,9 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<C, U, F, T> InboundUpgrade<C> for MapOutboundUpgrade<U, F>
|
impl<C, U, F> InboundUpgrade<C> for MapOutboundUpgrade<U, F>
|
||||||
where
|
where
|
||||||
U: InboundUpgrade<C>,
|
U: InboundUpgrade<C>
|
||||||
F: FnOnce(U::Output) -> T
|
|
||||||
{
|
{
|
||||||
type Output = U::Output;
|
type Output = U::Output;
|
||||||
type Error = U::Error;
|
type Error = U::Error;
|
||||||
@ -129,15 +127,15 @@ where
|
|||||||
|
|
||||||
/// Wraps around an upgrade and applies a closure to the error.
|
/// Wraps around an upgrade and applies a closure to the error.
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct MapUpgradeErr<U, F> { upgrade: U, fun: F }
|
pub struct MapInboundUpgradeErr<U, F> { upgrade: U, fun: F }
|
||||||
|
|
||||||
impl<U, F> MapUpgradeErr<U, F> {
|
impl<U, F> MapInboundUpgradeErr<U, F> {
|
||||||
pub fn new(upgrade: U, fun: F) -> Self {
|
pub fn new(upgrade: U, fun: F) -> Self {
|
||||||
MapUpgradeErr { upgrade, fun }
|
MapInboundUpgradeErr { upgrade, fun }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<U, F> UpgradeInfo for MapUpgradeErr<U, F>
|
impl<U, F> UpgradeInfo for MapInboundUpgradeErr<U, F>
|
||||||
where
|
where
|
||||||
U: UpgradeInfo
|
U: UpgradeInfo
|
||||||
{
|
{
|
||||||
@ -149,7 +147,7 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<C, U, F, T> InboundUpgrade<C> for MapUpgradeErr<U, F>
|
impl<C, U, F, T> InboundUpgrade<C> for MapInboundUpgradeErr<U, F>
|
||||||
where
|
where
|
||||||
U: InboundUpgrade<C>,
|
U: InboundUpgrade<C>,
|
||||||
F: FnOnce(U::Error) -> T
|
F: FnOnce(U::Error) -> T
|
||||||
@ -166,10 +164,45 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<C, U, F, T> OutboundUpgrade<C> for MapUpgradeErr<U, F>
|
impl<C, U, F> OutboundUpgrade<C> for MapInboundUpgradeErr<U, F>
|
||||||
|
where
|
||||||
|
U: OutboundUpgrade<C>
|
||||||
|
{
|
||||||
|
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<U, F> { upgrade: U, fun: F }
|
||||||
|
|
||||||
|
impl<U, F> MapOutboundUpgradeErr<U, F> {
|
||||||
|
pub fn new(upgrade: U, fun: F) -> Self {
|
||||||
|
MapOutboundUpgradeErr { upgrade, fun }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<U, F> UpgradeInfo for MapOutboundUpgradeErr<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> OutboundUpgrade<C> for MapOutboundUpgradeErr<U, F>
|
||||||
where
|
where
|
||||||
U: OutboundUpgrade<C>,
|
U: OutboundUpgrade<C>,
|
||||||
F: FnOnce(U::Error) -> T,
|
F: FnOnce(U::Error) -> T
|
||||||
{
|
{
|
||||||
type Output = U::Output;
|
type Output = U::Output;
|
||||||
type Error = T;
|
type Error = T;
|
||||||
@ -183,6 +216,19 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<C, U, F> InboundUpgrade<C> for MapOutboundUpgradeErr<U, F>
|
||||||
|
where
|
||||||
|
U: InboundUpgrade<C>
|
||||||
|
{
|
||||||
|
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<TInnerFut, TMap> {
|
pub struct MapFuture<TInnerFut, TMap> {
|
||||||
inner: TInnerFut,
|
inner: TInnerFut,
|
||||||
map: Option<TMap>,
|
map: Option<TMap>,
|
||||||
|
@ -71,7 +71,7 @@ pub use self::{
|
|||||||
apply::{apply, apply_inbound, apply_outbound, InboundUpgradeApply, OutboundUpgradeApply},
|
apply::{apply, apply_inbound, apply_outbound, InboundUpgradeApply, OutboundUpgradeApply},
|
||||||
denied::DeniedUpgrade,
|
denied::DeniedUpgrade,
|
||||||
error::UpgradeError,
|
error::UpgradeError,
|
||||||
map::{MapInboundUpgrade, MapOutboundUpgrade, MapUpgradeErr},
|
map::{MapInboundUpgrade, MapOutboundUpgrade, MapInboundUpgradeErr, MapOutboundUpgradeErr},
|
||||||
or::OrUpgrade,
|
or::OrUpgrade,
|
||||||
toggleable::{toggleable, Toggleable}
|
toggleable::{toggleable, Toggleable}
|
||||||
};
|
};
|
||||||
@ -119,12 +119,12 @@ pub trait InboundUpgradeExt<C>: InboundUpgrade<C> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Returns a new object that wraps around `Self` and applies a closure to the `Error`.
|
/// Returns a new object that wraps around `Self` and applies a closure to the `Error`.
|
||||||
fn map_inbound_err<F, T>(self, f: F) -> MapUpgradeErr<Self, F>
|
fn map_inbound_err<F, T>(self, f: F) -> MapInboundUpgradeErr<Self, F>
|
||||||
where
|
where
|
||||||
Self: Sized,
|
Self: Sized,
|
||||||
F: FnOnce(Self::Error) -> T
|
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
|
/// Returns a new object that combines `Self` and another upgrade to support both at the same
|
||||||
@ -169,12 +169,12 @@ pub trait OutboundUpgradeExt<C>: OutboundUpgrade<C> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Returns a new object that wraps around `Self` and applies a closure to the `Error`.
|
/// Returns a new object that wraps around `Self` and applies a closure to the `Error`.
|
||||||
fn map_outbound_err<F, T>(self, f: F) -> MapUpgradeErr<Self, F>
|
fn map_outbound_err<F, T>(self, f: F) -> MapOutboundUpgradeErr<Self, F>
|
||||||
where
|
where
|
||||||
Self: Sized,
|
Self: Sized,
|
||||||
F: FnOnce(Self::Error) -> T
|
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
|
/// Returns a new object that combines `Self` and another upgrade to support both at the same
|
||||||
|
Loading…
x
Reference in New Issue
Block a user