use super::Listener; use async_trait::async_trait; use std::io; /// Represents a [`Listener`] that wraps a different [`Listener`], /// mapping the received connection to something else using the map function pub struct MappedListener where L: Listener, F: FnMut(T) -> U + Send + Sync, { listener: L, f: F, } impl MappedListener where L: Listener, F: FnMut(T) -> U + Send + Sync, { pub fn new(listener: L, f: F) -> Self { Self { listener, f } } } #[async_trait] impl Listener for MappedListener where L: Listener, F: FnMut(T) -> U + Send + Sync, { type Output = U; /// Waits for the next fully-initialized transport for an incoming stream to be available, /// returning an error if no longer accepting new connections async fn accept(&mut self) -> io::Result { let output = self.listener.accept().await?; Ok((self.f)(output)) } }