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