libp2p_swarm/handler/
map_in.rs1use crate::handler::{
22 ConnectionEvent, ConnectionHandler, ConnectionHandlerEvent, 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 type InboundProtocol = TConnectionHandler::InboundProtocol;
56 type OutboundProtocol = TConnectionHandler::OutboundProtocol;
57 type InboundOpenInfo = TConnectionHandler::InboundOpenInfo;
58 type OutboundOpenInfo = TConnectionHandler::OutboundOpenInfo;
59
60 fn listen_protocol(&self) -> SubstreamProtocol<Self::InboundProtocol, Self::InboundOpenInfo> {
61 self.inner.listen_protocol()
62 }
63
64 fn on_behaviour_event(&mut self, event: TNewIn) {
65 if let Some(event) = (self.map)(event) {
66 self.inner.on_behaviour_event(event);
67 }
68 }
69
70 fn connection_keep_alive(&self) -> bool {
71 self.inner.connection_keep_alive()
72 }
73
74 fn poll(
75 &mut self,
76 cx: &mut Context<'_>,
77 ) -> Poll<
78 ConnectionHandlerEvent<Self::OutboundProtocol, Self::OutboundOpenInfo, Self::ToBehaviour>,
79 > {
80 self.inner.poll(cx)
81 }
82
83 fn poll_close(&mut self, cx: &mut Context<'_>) -> Poll<Option<Self::ToBehaviour>> {
84 self.inner.poll_close(cx)
85 }
86
87 fn on_connection_event(
88 &mut self,
89 event: ConnectionEvent<
90 Self::InboundProtocol,
91 Self::OutboundProtocol,
92 Self::InboundOpenInfo,
93 Self::OutboundOpenInfo,
94 >,
95 ) {
96 self.inner.on_connection_event(event);
97 }
98}