cs_mwc_libp2p_swarm/protocols_handler/
map_in.rs1use 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
32pub 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 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}