libp2p_swarm/handler/
map_out.rs1use crate::handler::{
22 ConnectionEvent, ConnectionHandler, ConnectionHandlerEvent, KeepAlive, SubstreamProtocol,
23};
24use std::fmt::Debug;
25use std::task::{Context, Poll};
26
27#[derive(Debug)]
29pub struct MapOutEvent<TConnectionHandler, TMap> {
30 inner: TConnectionHandler,
31 map: TMap,
32}
33
34impl<TConnectionHandler, TMap> MapOutEvent<TConnectionHandler, TMap> {
35 pub(crate) fn new(inner: TConnectionHandler, map: TMap) -> Self {
37 MapOutEvent { inner, map }
38 }
39}
40
41impl<TConnectionHandler, TMap, TNewOut> ConnectionHandler for MapOutEvent<TConnectionHandler, TMap>
42where
43 TConnectionHandler: ConnectionHandler,
44 TMap: FnMut(TConnectionHandler::ToBehaviour) -> TNewOut,
45 TNewOut: Debug + Send + 'static,
46 TMap: Send + 'static,
47{
48 type FromBehaviour = TConnectionHandler::FromBehaviour;
49 type ToBehaviour = TNewOut;
50 #[allow(deprecated)]
51 type Error = TConnectionHandler::Error;
52 type InboundProtocol = TConnectionHandler::InboundProtocol;
53 type OutboundProtocol = TConnectionHandler::OutboundProtocol;
54 type InboundOpenInfo = TConnectionHandler::InboundOpenInfo;
55 type OutboundOpenInfo = TConnectionHandler::OutboundOpenInfo;
56
57 fn listen_protocol(&self) -> SubstreamProtocol<Self::InboundProtocol, Self::InboundOpenInfo> {
58 self.inner.listen_protocol()
59 }
60
61 fn on_behaviour_event(&mut self, event: Self::FromBehaviour) {
62 self.inner.on_behaviour_event(event)
63 }
64
65 fn connection_keep_alive(&self) -> KeepAlive {
66 self.inner.connection_keep_alive()
67 }
68
69 #[allow(deprecated)]
70 fn poll(
71 &mut self,
72 cx: &mut Context<'_>,
73 ) -> Poll<
74 ConnectionHandlerEvent<
75 Self::OutboundProtocol,
76 Self::OutboundOpenInfo,
77 Self::ToBehaviour,
78 Self::Error,
79 >,
80 > {
81 self.inner.poll(cx).map(|ev| match ev {
82 ConnectionHandlerEvent::NotifyBehaviour(ev) => {
83 ConnectionHandlerEvent::NotifyBehaviour((self.map)(ev))
84 }
85 #[allow(deprecated)]
86 ConnectionHandlerEvent::Close(err) => ConnectionHandlerEvent::Close(err),
87 ConnectionHandlerEvent::OutboundSubstreamRequest { protocol } => {
88 ConnectionHandlerEvent::OutboundSubstreamRequest { protocol }
89 }
90 ConnectionHandlerEvent::ReportRemoteProtocols(support) => {
91 ConnectionHandlerEvent::ReportRemoteProtocols(support)
92 }
93 })
94 }
95
96 fn on_connection_event(
97 &mut self,
98 event: ConnectionEvent<
99 Self::InboundProtocol,
100 Self::OutboundProtocol,
101 Self::InboundOpenInfo,
102 Self::OutboundOpenInfo,
103 >,
104 ) {
105 self.inner.on_connection_event(event);
106 }
107}