cs_mwc_libp2p_swarm/protocols_handler/
map_out.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::task::{Context, Poll};
31
32pub struct MapOutEvent<TProtoHandler, TMap> {
34 inner: TProtoHandler,
35 map: TMap,
36}
37
38impl<TProtoHandler, TMap> MapOutEvent<TProtoHandler, TMap> {
39 pub(crate) fn new(inner: TProtoHandler, map: TMap) -> Self {
41 MapOutEvent {
42 inner,
43 map,
44 }
45 }
46}
47
48impl<TProtoHandler, TMap, TNewOut> ProtocolsHandler for MapOutEvent<TProtoHandler, TMap>
49where
50 TProtoHandler: ProtocolsHandler,
51 TMap: FnMut(TProtoHandler::OutEvent) -> TNewOut,
52 TNewOut: Send + 'static,
53 TMap: Send + 'static,
54{
55 type InEvent = TProtoHandler::InEvent;
56 type OutEvent = TNewOut;
57 type Error = TProtoHandler::Error;
58 type InboundProtocol = TProtoHandler::InboundProtocol;
59 type OutboundProtocol = TProtoHandler::OutboundProtocol;
60 type InboundOpenInfo = TProtoHandler::InboundOpenInfo;
61 type OutboundOpenInfo = TProtoHandler::OutboundOpenInfo;
62
63 fn listen_protocol(&self) -> SubstreamProtocol<Self::InboundProtocol, Self::InboundOpenInfo> {
64 self.inner.listen_protocol()
65 }
66
67 fn inject_fully_negotiated_inbound(
68 &mut self,
69 protocol: <Self::InboundProtocol as InboundUpgradeSend>::Output,
70 info: Self::InboundOpenInfo
71 ) {
72 self.inner.inject_fully_negotiated_inbound(protocol, info)
73 }
74
75 fn inject_fully_negotiated_outbound(
76 &mut self,
77 protocol: <Self::OutboundProtocol as OutboundUpgradeSend>::Output,
78 info: Self::OutboundOpenInfo
79 ) {
80 self.inner.inject_fully_negotiated_outbound(protocol, info)
81 }
82
83 fn inject_event(&mut self, event: Self::InEvent) {
84 self.inner.inject_event(event)
85 }
86
87 fn inject_address_change(&mut self, addr: &Multiaddr) {
88 self.inner.inject_address_change(addr)
89 }
90
91 fn inject_dial_upgrade_error(&mut self, info: Self::OutboundOpenInfo, error: ProtocolsHandlerUpgrErr<<Self::OutboundProtocol as OutboundUpgradeSend>::Error>) {
92 self.inner.inject_dial_upgrade_error(info, error)
93 }
94
95 fn inject_listen_upgrade_error(&mut self, info: Self::InboundOpenInfo, error: ProtocolsHandlerUpgrErr<<Self::InboundProtocol as InboundUpgradeSend>::Error>) {
96 self.inner.inject_listen_upgrade_error(info, error)
97 }
98
99 fn connection_keep_alive(&self) -> KeepAlive {
100 self.inner.connection_keep_alive()
101 }
102
103 fn poll(
104 &mut self,
105 cx: &mut Context<'_>,
106 ) -> Poll<
107 ProtocolsHandlerEvent<Self::OutboundProtocol, Self::OutboundOpenInfo, Self::OutEvent, Self::Error>,
108 > {
109 self.inner.poll(cx).map(|ev| {
110 match ev {
111 ProtocolsHandlerEvent::Custom(ev) => ProtocolsHandlerEvent::Custom((self.map)(ev)),
112 ProtocolsHandlerEvent::Close(err) => ProtocolsHandlerEvent::Close(err),
113 ProtocolsHandlerEvent::OutboundSubstreamRequest { protocol } => {
114 ProtocolsHandlerEvent::OutboundSubstreamRequest { protocol }
115 }
116 }
117 })
118 }
119}