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