You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
distant/distant-net/src/common/listener/mapped.rs

41 lines
1.0 KiB
Rust

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<L, F, T, U>
where
L: Listener<Output = T>,
F: FnMut(T) -> U + Send + Sync,
{
listener: L,
f: F,
}
impl<L, F, T, U> MappedListener<L, F, T, U>
where
L: Listener<Output = T>,
F: FnMut(T) -> U + Send + Sync,
{
pub fn new(listener: L, f: F) -> Self {
Self { listener, f }
}
}
#[async_trait]
impl<L, F, T, U> Listener for MappedListener<L, F, T, U>
where
L: Listener<Output = T>,
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<Self::Output> {
let output = self.listener.accept().await?;
Ok((self.f)(output))
}
}