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
46#[expect(deprecated)] // TODO: Remove when {In, Out}boundOpenInfo is fully removed.
47impl<TConnectionHandler, TMap, TNewOut> ConnectionHandler for MapOutEvent<TConnectionHandler, TMap>
48where
49    TConnectionHandler: ConnectionHandler,
50    TMap: FnMut(TConnectionHandler::ToBehaviour) -> TNewOut,
51    TNewOut: Debug + Send + 'static,
52    TMap: Send + 'static,
53{
54    type FromBehaviour = TConnectionHandler::FromBehaviour;
55    type ToBehaviour = TNewOut;
56    type InboundProtocol = TConnectionHandler::InboundProtocol;
57    type OutboundProtocol = TConnectionHandler::OutboundProtocol;
58    type InboundOpenInfo = TConnectionHandler::InboundOpenInfo;
59    type OutboundOpenInfo = TConnectionHandler::OutboundOpenInfo;
60
61    fn listen_protocol(&self) -> SubstreamProtocol<Self::InboundProtocol, Self::InboundOpenInfo> {
62        self.inner.listen_protocol()
63    }
64
65    fn on_behaviour_event(&mut self, event: Self::FromBehaviour) {
66        self.inner.on_behaviour_event(event)
67    }
68
69    fn connection_keep_alive(&self) -> bool {
70        self.inner.connection_keep_alive()
71    }
72
73    fn poll(
74        &mut self,
75        cx: &mut Context<'_>,
76    ) -> Poll<
77        ConnectionHandlerEvent<Self::OutboundProtocol, Self::OutboundOpenInfo, Self::ToBehaviour>,
78    > {
79        self.inner.poll(cx).map(|ev| match ev {
80            ConnectionHandlerEvent::NotifyBehaviour(ev) => {
81                ConnectionHandlerEvent::NotifyBehaviour((self.map)(ev))
82            }
83            ConnectionHandlerEvent::OutboundSubstreamRequest { protocol } => {
84                ConnectionHandlerEvent::OutboundSubstreamRequest { protocol }
85            }
86            ConnectionHandlerEvent::ReportRemoteProtocols(support) => {
87                ConnectionHandlerEvent::ReportRemoteProtocols(support)
88            }
89        })
90    }
91
92    fn poll_close(&mut self, cx: &mut Context<'_>) -> Poll<Option<Self::ToBehaviour>> {
93        let Some(e) = ready!(self.inner.poll_close(cx)) else {
94            return Poll::Ready(None);
95        };
96
97        Poll::Ready(Some((self.map)(e)))
98    }
99
100    fn on_connection_event(
101        &mut self,
102        event: ConnectionEvent<
103            Self::InboundProtocol,
104            Self::OutboundProtocol,
105            Self::InboundOpenInfo,
106            Self::OutboundOpenInfo,
107        >,
108    ) {
109        self.inner.on_connection_event(event);
110    }
111}