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 std::{
22    fmt::Debug,
23    task::{Context, Poll},
24};
25
26use futures::ready;
27
28use crate::handler::{
29    ConnectionEvent, ConnectionHandler, ConnectionHandlerEvent, SubstreamProtocol,
30};
31
32/// Wrapper around a protocol handler that turns the output event into something else.
33#[derive(Debug)]
34pub struct MapOutEvent<TConnectionHandler, TMap> {
35    inner: TConnectionHandler,
36    map: TMap,
37}
38
39impl<TConnectionHandler, TMap> MapOutEvent<TConnectionHandler, TMap> {
40    /// Creates a `MapOutEvent`.
41    pub(crate) fn new(inner: TConnectionHandler, map: TMap) -> Self {
42        MapOutEvent { inner, map }
43    }
44}
45
46impl<TConnectionHandler, TMap, TNewOut> ConnectionHandler for MapOutEvent<TConnectionHandler, TMap>
47where
48    TConnectionHandler: ConnectionHandler,
49    TMap: FnMut(TConnectionHandler::ToBehaviour) -> TNewOut,
50    TNewOut: Debug + Send + 'static,
51    TMap: Send + 'static,
52{
53    type FromBehaviour = TConnectionHandler::FromBehaviour;
54    type ToBehaviour = TNewOut;
55    type InboundProtocol = TConnectionHandler::InboundProtocol;
56    type OutboundProtocol = TConnectionHandler::OutboundProtocol;
57    type InboundOpenInfo = TConnectionHandler::InboundOpenInfo;
58    type OutboundOpenInfo = TConnectionHandler::OutboundOpenInfo;
59
60    fn listen_protocol(&self) -> SubstreamProtocol<Self::InboundProtocol, Self::InboundOpenInfo> {
61        self.inner.listen_protocol()
62    }
63
64    fn on_behaviour_event(&mut self, event: Self::FromBehaviour) {
65        self.inner.on_behaviour_event(event)
66    }
67
68    fn connection_keep_alive(&self) -> bool {
69        self.inner.connection_keep_alive()
70    }
71
72    fn poll(
73        &mut self,
74        cx: &mut Context<'_>,
75    ) -> Poll<
76        ConnectionHandlerEvent<Self::OutboundProtocol, Self::OutboundOpenInfo, Self::ToBehaviour>,
77    > {
78        self.inner.poll(cx).map(|ev| match ev {
79            ConnectionHandlerEvent::NotifyBehaviour(ev) => {
80                ConnectionHandlerEvent::NotifyBehaviour((self.map)(ev))
81            }
82            ConnectionHandlerEvent::OutboundSubstreamRequest { protocol } => {
83                ConnectionHandlerEvent::OutboundSubstreamRequest { protocol }
84            }
85            ConnectionHandlerEvent::ReportRemoteProtocols(support) => {
86                ConnectionHandlerEvent::ReportRemoteProtocols(support)
87            }
88        })
89    }
90
91    fn poll_close(&mut self, cx: &mut Context<'_>) -> Poll<Option<Self::ToBehaviour>> {
92        let Some(e) = ready!(self.inner.poll_close(cx)) else {
93            return Poll::Ready(None);
94        };
95
96        Poll::Ready(Some((self.map)(e)))
97    }
98
99    fn on_connection_event(
100        &mut self,
101        event: ConnectionEvent<
102            Self::InboundProtocol,
103            Self::OutboundProtocol,
104            Self::InboundOpenInfo,
105            Self::OutboundOpenInfo,
106        >,
107    ) {
108        self.inner.on_connection_event(event);
109    }
110}