cs_mwc_libp2p_swarm/protocols_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 crate::upgrade::{InboundUpgradeSend, OutboundUpgradeSend};
22use crate::protocols_handler::{
23    KeepAlive,
24    SubstreamProtocol,
25    ProtocolsHandler,
26    ProtocolsHandlerEvent,
27    ProtocolsHandlerUpgrErr
28};
29use mwc_libp2p_core::Multiaddr;
30use std::{marker::PhantomData, task::Context, task::Poll};
31
32/// Wrapper around a protocol handler that turns the input event into something else.
33pub struct MapInEvent<TProtoHandler, TNewIn, TMap> {
34    inner: TProtoHandler,
35    map: TMap,
36    marker: PhantomData<TNewIn>,
37}
38
39impl<TProtoHandler, TMap, TNewIn> MapInEvent<TProtoHandler, TNewIn, TMap> {
40    /// Creates a `MapInEvent`.
41    pub(crate) fn new(inner: TProtoHandler, map: TMap) -> Self {
42        MapInEvent {
43            inner,
44            map,
45            marker: PhantomData,
46        }
47    }
48}
49
50impl<TProtoHandler, TMap, TNewIn> ProtocolsHandler for MapInEvent<TProtoHandler, TNewIn, TMap>
51where
52    TProtoHandler: ProtocolsHandler,
53    TMap: Fn(TNewIn) -> Option<TProtoHandler::InEvent>,
54    TNewIn: Send + 'static,
55    TMap: Send + 'static,
56{
57    type InEvent = TNewIn;
58    type OutEvent = TProtoHandler::OutEvent;
59    type Error = TProtoHandler::Error;
60    type InboundProtocol = TProtoHandler::InboundProtocol;
61    type OutboundProtocol = TProtoHandler::OutboundProtocol;
62    type InboundOpenInfo = TProtoHandler::InboundOpenInfo;
63    type OutboundOpenInfo = TProtoHandler::OutboundOpenInfo;
64
65    fn listen_protocol(&self) -> SubstreamProtocol<Self::InboundProtocol, Self::InboundOpenInfo> {
66        self.inner.listen_protocol()
67    }
68
69    fn inject_fully_negotiated_inbound(
70        &mut self,
71        protocol: <Self::InboundProtocol as InboundUpgradeSend>::Output,
72        info: Self::InboundOpenInfo
73    ) {
74        self.inner.inject_fully_negotiated_inbound(protocol, info)
75    }
76
77    fn inject_fully_negotiated_outbound(
78        &mut self,
79        protocol: <Self::OutboundProtocol as OutboundUpgradeSend>::Output,
80        info: Self::OutboundOpenInfo
81    ) {
82        self.inner.inject_fully_negotiated_outbound(protocol, info)
83    }
84
85    fn inject_event(&mut self, event: TNewIn) {
86        if let Some(event) = (self.map)(event) {
87            self.inner.inject_event(event);
88        }
89    }
90
91    fn inject_address_change(&mut self, addr: &Multiaddr) {
92        self.inner.inject_address_change(addr)
93    }
94
95    fn inject_dial_upgrade_error(&mut self, info: Self::OutboundOpenInfo, error: ProtocolsHandlerUpgrErr<<Self::OutboundProtocol as OutboundUpgradeSend>::Error>) {
96        self.inner.inject_dial_upgrade_error(info, error)
97    }
98
99    fn inject_listen_upgrade_error(&mut self, info: Self::InboundOpenInfo, error: ProtocolsHandlerUpgrErr<<Self::InboundProtocol as InboundUpgradeSend>::Error>) {
100        self.inner.inject_listen_upgrade_error(info, error)
101    }
102
103    fn connection_keep_alive(&self) -> KeepAlive {
104        self.inner.connection_keep_alive()
105    }
106
107    fn poll(
108        &mut self,
109        cx: &mut Context<'_>,
110    ) -> Poll<
111        ProtocolsHandlerEvent<Self::OutboundProtocol, Self::OutboundOpenInfo, Self::OutEvent, Self::Error>,
112    > {
113        self.inner.poll(cx)
114    }
115}