diff --git a/core/src/transport/map_err.rs b/core/src/transport/map_err.rs new file mode 100644 index 00000000..975c02bb --- /dev/null +++ b/core/src/transport/map_err.rs @@ -0,0 +1,114 @@ +// Copyright 2018 Parity Technologies (UK) Ltd. +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the "Software"), +// to deal in the Software without restriction, including without limitation +// the rights to use, copy, modify, merge, publish, distribute, sublicense, +// and/or sell copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. + +use futures::prelude::*; +use multiaddr::Multiaddr; +use std::io::Error as IoError; +use transport::{MuxedTransport, Transport}; + +/// See `Transport::map_err`. +#[derive(Debug, Copy, Clone)] +pub struct MapErr { + transport: T, + map: F, +} + +impl MapErr { + /// Internal function that builds a `MapErr`. + #[inline] + pub(crate) fn new(transport: T, map: F) -> MapErr { + MapErr { transport, map } + } +} + +impl Transport for MapErr +where + T: Transport + 'static, // TODO: 'static :-/ + F: FnOnce(IoError) -> IoError + Clone + 'static, // TODO: 'static :-/ +{ + type Output = T::Output; + type MultiaddrFuture = T::MultiaddrFuture; + type Listener = Box>; + type ListenerUpgrade = + Box>; + type Dial = Box>; + + fn listen_on(self, addr: Multiaddr) -> Result<(Self::Listener, Multiaddr), (Self, Multiaddr)> { + let map = self.map; + + match self.transport.listen_on(addr) { + Ok((stream, listen_addr)) => { + let map2 = map.clone(); + let stream = stream + .map(move |future| { + let map = map.clone(); + let future = future.into_future().map_err(move |err| map(err)); + Box::new(future) as Box<_> + }) + .map_err(move |err| { + let map = map2.clone(); + map(err) + }); + Ok((Box::new(stream), listen_addr)) + } + Err((transport, addr)) => Err((MapErr { transport, map }, addr)), + } + } + + fn dial(self, addr: Multiaddr) -> Result { + let map = self.map; + + match self.transport.dial(addr) { + Ok(future) => { + let future = future.into_future().map_err(move |err| map(err)); + Ok(Box::new(future)) + } + Err((transport, addr)) => Err((MapErr { transport, map }, addr)), + } + } + + #[inline] + fn nat_traversal(&self, server: &Multiaddr, observed: &Multiaddr) -> Option { + self.transport.nat_traversal(server, observed) + } +} + +impl MuxedTransport for MapErr +where + T: MuxedTransport + 'static, // TODO: 'static :-/ + F: FnOnce(IoError) -> IoError + Clone + 'static, // TODO: 'static :-/ +{ + type Incoming = Box>; + type IncomingUpgrade = + Box>; + + fn next_incoming(self) -> Self::Incoming { + let map = self.map; + let map2 = map.clone(); + let future = self.transport + .next_incoming() + .map(move |upgrade| { + let future = upgrade.map_err(map); + Box::new(future) as Box<_> + }) + .map_err(map2); + Box::new(future) + } +} diff --git a/core/src/transport/mod.rs b/core/src/transport/mod.rs index 958b9404..c0e2048f 100644 --- a/core/src/transport/mod.rs +++ b/core/src/transport/mod.rs @@ -40,6 +40,7 @@ pub mod choice; pub mod denied; pub mod dummy; pub mod map; +pub mod map_err; pub mod memory; pub mod muxed; pub mod upgrade; @@ -126,6 +127,16 @@ pub trait Transport { map::Map::new(self, map) } + /// Applies a function on the errors generated by the futures of the `Transport`. + #[inline] + fn map_err(self, map_err: F) -> map_err::MapErr + where + Self: Sized, + F: FnOnce(IoError) -> IoError + Clone + 'static, // TODO: 'static :-/ + { + map_err::MapErr::new(self, map_err) + } + /// Builds a new struct that implements `Transport` that contains both `self` and `other`. /// /// The returned object will redirect its calls to `self`, except that if `listen_on` or `dial`