use crate::handler::{
ConnectionEvent, ConnectionHandler, ConnectionHandlerEvent, SubstreamProtocol,
};
use std::{fmt::Debug, marker::PhantomData, task::Context, task::Poll};
#[derive(Debug)]
pub struct MapInEvent<TConnectionHandler, TNewIn, TMap> {
inner: TConnectionHandler,
map: TMap,
marker: PhantomData<TNewIn>,
}
impl<TConnectionHandler, TMap, TNewIn> MapInEvent<TConnectionHandler, TNewIn, TMap> {
pub(crate) fn new(inner: TConnectionHandler, map: TMap) -> Self {
MapInEvent {
inner,
map,
marker: PhantomData,
}
}
}
impl<TConnectionHandler, TMap, TNewIn> ConnectionHandler
for MapInEvent<TConnectionHandler, TNewIn, TMap>
where
TConnectionHandler: ConnectionHandler,
TMap: Fn(TNewIn) -> Option<TConnectionHandler::FromBehaviour>,
TNewIn: Debug + Send + 'static,
TMap: Send + 'static,
{
type FromBehaviour = TNewIn;
type ToBehaviour = TConnectionHandler::ToBehaviour;
type InboundProtocol = TConnectionHandler::InboundProtocol;
type OutboundProtocol = TConnectionHandler::OutboundProtocol;
type InboundOpenInfo = TConnectionHandler::InboundOpenInfo;
type OutboundOpenInfo = TConnectionHandler::OutboundOpenInfo;
fn listen_protocol(&self) -> SubstreamProtocol<Self::InboundProtocol, Self::InboundOpenInfo> {
self.inner.listen_protocol()
}
fn on_behaviour_event(&mut self, event: TNewIn) {
if let Some(event) = (self.map)(event) {
self.inner.on_behaviour_event(event);
}
}
fn connection_keep_alive(&self) -> bool {
self.inner.connection_keep_alive()
}
fn poll(
&mut self,
cx: &mut Context<'_>,
) -> Poll<
ConnectionHandlerEvent<Self::OutboundProtocol, Self::OutboundOpenInfo, Self::ToBehaviour>,
> {
self.inner.poll(cx)
}
fn poll_close(&mut self, cx: &mut Context<'_>) -> Poll<Option<Self::ToBehaviour>> {
self.inner.poll_close(cx)
}
fn on_connection_event(
&mut self,
event: ConnectionEvent<
Self::InboundProtocol,
Self::OutboundProtocol,
Self::InboundOpenInfo,
Self::OutboundOpenInfo,
>,
) {
self.inner.on_connection_event(event);
}
}