use rama_core::{
Service,
error::{BoxError, ErrorContext as _},
io::Io,
};
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct DiscardService;
impl DiscardService {
#[must_use]
#[inline(always)]
pub const fn new() -> Self {
Self
}
}
impl Default for DiscardService {
#[inline(always)]
fn default() -> Self {
Self::new()
}
}
impl<S> Service<S> for DiscardService
where
S: Io + 'static,
{
type Output = u64;
type Error = BoxError;
async fn serve(&self, stream: S) -> Result<Self::Output, Self::Error> {
let (mut reader, _) = tokio::io::split(stream);
let mut writer = tokio::io::empty();
tokio::io::copy(&mut reader, &mut writer)
.await
.into_box_error()
}
}
#[cfg(test)]
mod tests {
use super::*;
use tokio_test::io::Builder;
#[tokio::test]
async fn test_echo() {
let stream = Builder::new().read(b"one").read(b"two").build();
DiscardService::new().serve(stream).await.unwrap();
}
}