use std::io;
use tokio::io::{AsyncRead, AsyncWrite};
use super::{FrameReader, FrameWriter};
use crate::Schema;
use crate::wire::io::framing::frame_len;
pub async fn write_frame<T: Schema>(
w: &mut (impl AsyncWrite + Unpin),
message: &T,
) -> io::Result<()> {
FrameWriter::new().send(w, message).await
}
pub async fn write_frame_bytes(w: &mut (impl AsyncWrite + Unpin), bytes: &[u8]) -> io::Result<()> {
use tokio::io::AsyncWriteExt;
let len = frame_len(bytes.len())?;
let mut frame = Vec::with_capacity(size_of::<crate::io::Len>() + bytes.len());
frame.extend_from_slice(&len.to_le_bytes());
frame.extend_from_slice(bytes);
w.write_all(&frame).await?;
w.flush().await
}
pub async fn read_frame_bytes(r: &mut (impl AsyncRead + Unpin)) -> io::Result<Option<Vec<u8>>> {
FrameReader::new().read_owned(r).await
}
pub async fn read_frame<T: Schema>(r: &mut (impl AsyncRead + Unpin)) -> io::Result<Option<T>> {
FrameReader::new().read(r).await
}