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
50#[expect(deprecated)] impl<TConnectionHandler, TMap, TNewIn> ConnectionHandler
52 for MapInEvent<TConnectionHandler, TNewIn, TMap>
53where
54 TConnectionHandler: ConnectionHandler,
55 TMap: Fn(TNewIn) -> Option<TConnectionHandler::FromBehaviour>,
56 TNewIn: Debug + Send + 'static,
57 TMap: Send + 'static,
58{
59 type FromBehaviour = TNewIn;
60 type ToBehaviour = TConnectionHandler::ToBehaviour;
61 type InboundProtocol = TConnectionHandler::InboundProtocol;
62 type OutboundProtocol = TConnectionHandler::OutboundProtocol;
63 type InboundOpenInfo = TConnectionHandler::InboundOpenInfo;
64 type OutboundOpenInfo = TConnectionHandler::OutboundOpenInfo;
65
66 fn listen_protocol(&self) -> SubstreamProtocol<Self::InboundProtocol, Self::InboundOpenInfo> {
67 self.inner.listen_protocol()
68 }
69
70 fn on_behaviour_event(&mut self, event: TNewIn) {
71 if let Some(event) = (self.map)(event) {
72 self.inner.on_behaviour_event(event);
73 }
74 }
75
76 fn connection_keep_alive(&self) -> bool {
77 self.inner.connection_keep_alive()
78 }
79
80 fn poll(
81 &mut self,
82 cx: &mut Context<'_>,
83 ) -> Poll<
84 ConnectionHandlerEvent<Self::OutboundProtocol, Self::OutboundOpenInfo, Self::ToBehaviour>,
85 > {
86 self.inner.poll(cx)
87 }
88
89 fn poll_close(&mut self, cx: &mut Context<'_>) -> Poll<Option<Self::ToBehaviour>> {
90 self.inner.poll_close(cx)
91 }
92
93 fn on_connection_event(
94 &mut self,
95 event: ConnectionEvent<
96 Self::InboundProtocol,
97 Self::OutboundProtocol,
98 Self::InboundOpenInfo,
99 Self::OutboundOpenInfo,
100 >,
101 ) {
102 self.inner.on_connection_event(event);
103 }
104}