libp2p_swarm/handler/
map_in.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    marker::PhantomData,
24    task::{Context, Poll},
25};
26
27use crate::handler::{
28    ConnectionEvent, ConnectionHandler, ConnectionHandlerEvent, SubstreamProtocol,
29};
30
31/// Wrapper around a protocol handler that turns the input event into something else.
32#[derive(Debug)]
33pub struct MapInEvent<TConnectionHandler, TNewIn, TMap> {
34    inner: TConnectionHandler,
35    map: TMap,
36    marker: PhantomData<TNewIn>,
37}
38
39impl<TConnectionHandler, TMap, TNewIn> MapInEvent<TConnectionHandler, TNewIn, TMap> {
40    /// Creates a `MapInEvent`.
41    pub(crate) fn new(inner: TConnectionHandler, map: TMap) -> Self {
42        MapInEvent {
43            inner,
44            map,
45            marker: PhantomData,
46        }
47    }
48}
49
50#[expect(deprecated)] // TODO: Remove when {In, Out}boundOpenInfo is fully removed.
51impl<TConnectionHandler, TMap, TNewIn> ConnectionHandler
52    for MapInEvent<TConnectionHandler, TNewIn, TMap>
53where
54    TConnectionHandler: ConnectionHandler,
55    TMap: Fn(TNewIn) -> Option<TConnectionHandler::FromBehaviour>,
56    TNewIn: Debug + Send + 'static,
57    TMap: Send + 'static,
58{
59    type FromBehaviour = TNewIn;
60    type ToBehaviour = TConnectionHandler::ToBehaviour;
61    type InboundProtocol = TConnectionHandler::InboundProtocol;
62    type OutboundProtocol = TConnectionHandler::OutboundProtocol;
63    type InboundOpenInfo = TConnectionHandler::InboundOpenInfo;
64    type OutboundOpenInfo = TConnectionHandler::OutboundOpenInfo;
65
66    fn listen_protocol(&self) -> SubstreamProtocol<Self::InboundProtocol, Self::InboundOpenInfo> {
67        self.inner.listen_protocol()
68    }
69
70    fn on_behaviour_event(&mut self, event: TNewIn) {
71        if let Some(event) = (self.map)(event) {
72            self.inner.on_behaviour_event(event);
73        }
74    }
75
76    fn connection_keep_alive(&self) -> bool {
77        self.inner.connection_keep_alive()
78    }
79
80    fn poll(
81        &mut self,
82        cx: &mut Context<'_>,
83    ) -> Poll<
84        ConnectionHandlerEvent<Self::OutboundProtocol, Self::OutboundOpenInfo, Self::ToBehaviour>,
85    > {
86        self.inner.poll(cx)
87    }
88
89    fn poll_close(&mut self, cx: &mut Context<'_>) -> Poll<Option<Self::ToBehaviour>> {
90        self.inner.poll_close(cx)
91    }
92
93    fn on_connection_event(
94        &mut self,
95        event: ConnectionEvent<
96            Self::InboundProtocol,
97            Self::OutboundProtocol,
98            Self::InboundOpenInfo,
99            Self::OutboundOpenInfo,
100        >,
101    ) {
102        self.inner.on_connection_event(event);
103    }
104}