libp2p_swarm/handler/
map_out.rs

1// Copyright 2018 Parity Technologies (UK) Ltd.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a
4// copy of this software and associated documentation files (the "Software"),
5// to deal in the Software without restriction, including without limitation
6// the rights to use, copy, modify, merge, publish, distribute, sublicense,
7// and/or sell copies of the Software, and to permit persons to whom the
8// Software is furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in
11// all copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
14// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
19// DEALINGS IN THE SOFTWARE.
20
21use crate::handler::{
22    ConnectionEvent, ConnectionHandler, ConnectionHandlerEvent, KeepAlive, SubstreamProtocol,
23};
24use std::fmt::Debug;
25use std::task::{Context, Poll};
26
27/// Wrapper around a protocol handler that turns the output event into something else.
28#[derive(Debug)]
29pub struct MapOutEvent<TConnectionHandler, TMap> {
30    inner: TConnectionHandler,
31    map: TMap,
32}
33
34impl<TConnectionHandler, TMap> MapOutEvent<TConnectionHandler, TMap> {
35    /// Creates a `MapOutEvent`.
36    pub(crate) fn new(inner: TConnectionHandler, map: TMap) -> Self {
37        MapOutEvent { inner, map }
38    }
39}
40
41impl<TConnectionHandler, TMap, TNewOut> ConnectionHandler for MapOutEvent<TConnectionHandler, TMap>
42where
43    TConnectionHandler: ConnectionHandler,
44    TMap: FnMut(TConnectionHandler::ToBehaviour) -> TNewOut,
45    TNewOut: Debug + Send + 'static,
46    TMap: Send + 'static,
47{
48    type FromBehaviour = TConnectionHandler::FromBehaviour;
49    type ToBehaviour = TNewOut;
50    #[allow(deprecated)]
51    type Error = TConnectionHandler::Error;
52    type InboundProtocol = TConnectionHandler::InboundProtocol;
53    type OutboundProtocol = TConnectionHandler::OutboundProtocol;
54    type InboundOpenInfo = TConnectionHandler::InboundOpenInfo;
55    type OutboundOpenInfo = TConnectionHandler::OutboundOpenInfo;
56
57    fn listen_protocol(&self) -> SubstreamProtocol<Self::InboundProtocol, Self::InboundOpenInfo> {
58        self.inner.listen_protocol()
59    }
60
61    fn on_behaviour_event(&mut self, event: Self::FromBehaviour) {
62        self.inner.on_behaviour_event(event)
63    }
64
65    fn connection_keep_alive(&self) -> KeepAlive {
66        self.inner.connection_keep_alive()
67    }
68
69    #[allow(deprecated)]
70    fn poll(
71        &mut self,
72        cx: &mut Context<'_>,
73    ) -> Poll<
74        ConnectionHandlerEvent<
75            Self::OutboundProtocol,
76            Self::OutboundOpenInfo,
77            Self::ToBehaviour,
78            Self::Error,
79        >,
80    > {
81        self.inner.poll(cx).map(|ev| match ev {
82            ConnectionHandlerEvent::NotifyBehaviour(ev) => {
83                ConnectionHandlerEvent::NotifyBehaviour((self.map)(ev))
84            }
85            #[allow(deprecated)]
86            ConnectionHandlerEvent::Close(err) => ConnectionHandlerEvent::Close(err),
87            ConnectionHandlerEvent::OutboundSubstreamRequest { protocol } => {
88                ConnectionHandlerEvent::OutboundSubstreamRequest { protocol }
89            }
90            ConnectionHandlerEvent::ReportRemoteProtocols(support) => {
91                ConnectionHandlerEvent::ReportRemoteProtocols(support)
92            }
93        })
94    }
95
96    fn on_connection_event(
97        &mut self,
98        event: ConnectionEvent<
99            Self::InboundProtocol,
100            Self::OutboundProtocol,
101            Self::InboundOpenInfo,
102            Self::OutboundOpenInfo,
103        >,
104    ) {
105        self.inner.on_connection_event(event);
106    }
107}