use tokio::io::AsyncWriteExt as _;
pub struct Writer<T: tokio::io::AsyncWrite> {
output: T,
creator: crate::Creator,
}
impl<T: tokio::io::AsyncWrite + std::marker::Unpin + Send> Writer<T> {
pub fn new(output: T) -> Self {
Self {
output,
creator: crate::Creator::new(),
}
}
pub async fn frame(&mut self, data: &[u8]) -> crate::Result<()> {
self.frame_at(std::time::Instant::now(), data).await
}
pub async fn frame_at(
&mut self,
cur_time: std::time::Instant,
data: &[u8],
) -> crate::Result<()> {
let frame = self.creator.frame_at(cur_time, data);
let bytes: Vec<u8> = frame.try_into()?;
self.output
.write_all(&bytes)
.await
.map_err(|source| crate::Error::Write { source })
}
}