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