use std::io;
use tokio::io::{AsyncWrite, AsyncWriteExt};
use crate::Schema;
#[derive(Debug, Default)]
pub struct FrameWriter {
inner: crate::io::FrameWriter,
}
impl FrameWriter {
#[must_use]
pub fn new() -> Self {
Self::default()
}
pub async fn send<T: Schema>(
&mut self,
w: &mut (impl AsyncWrite + Unpin),
message: &T,
) -> io::Result<()> {
put(w, self.inner.named_frame(message)?).await
}
pub async fn send_packed<T: Schema>(
&mut self,
w: &mut (impl AsyncWrite + Unpin),
message: &T,
) -> io::Result<()> {
put(w, self.inner.packed_frame(message)?).await
}
}
async fn put(w: &mut (impl AsyncWrite + Unpin), frame: &[u8]) -> io::Result<()> {
w.write_all(frame).await?;
w.flush().await
}