feat(proto): add generic proto writer

This commit is contained in:
Jindřich Moravec 2023-12-11 16:49:30 +01:00
parent 65f90ba600
commit 0a3683e2fa
4 changed files with 69 additions and 0 deletions

View file

@ -0,0 +1,34 @@
use async_trait::async_trait;
use tokio::io::{AsyncWrite, AsyncWriteExt};
pub struct ProtoWriter<W>
where
W: AsyncWrite + Unpin + Send,
{
pub(super) inner: W,
}
impl<W> ProtoWriter<W>
where
W: AsyncWrite + Unpin + Send,
{
pub fn new(writer: W) -> ProtoWriter<W> {
ProtoWriter { inner: writer }
}
}
#[async_trait]
pub trait ProtoFlush {
async fn flush(&mut self) -> anyhow::Result<()>;
}
#[async_trait]
impl<W> ProtoFlush for ProtoWriter<W>
where
W: AsyncWrite + Unpin + Send,
{
async fn flush(&mut self) -> anyhow::Result<()> {
self.inner.flush().await?;
Ok(())
}
}