use core::task::Context;
use crate::buffer::BufferCursor;
use crate::{backend, io};
pub struct BufferState<const LEN: usize> {
pub cur: BufferCursor,
buf: [u8; LEN],
}
impl<const LEN: usize> BufferState<LEN> {
#[inline(always)]
pub fn with_contents(buf: [u8; LEN]) -> Self {
Self {
cur: BufferCursor::new(&buf),
buf,
}
}
#[inline(always)]
pub fn with_partial_contents(buf: [u8; LEN], n: usize) -> Self {
Self {
cur: BufferCursor::new(&buf[..n]),
buf,
}
}
#[inline(always)]
pub fn init() -> Self {
Self::with_contents([0u8; LEN])
}
#[inline(always)]
pub fn buffer(&self) -> &[u8; LEN] {
&self.buf
}
#[inline(always)]
pub fn is_pending(&self) -> bool {
self.cur.is_pending()
}
#[inline(always)]
pub fn len(&self) -> usize {
self.cur.len()
}
#[inline(always)]
pub fn is_empty(&self) -> bool {
self.cur.is_empty()
}
#[inline(always)]
pub fn extend_len(&mut self, n: usize) {
debug_assert!(LEN - self.cur.len() >= n);
self.cur.extend_len(::core::cmp::min(LEN - self.cur.len(), n));
}
pub fn start_write<W>(&mut self, writer: &mut W, cx: &mut Context<'_>) -> backend::PollEncodeStatus<io::Error>
where
W: io::AsyncWrite + Unpin,
{
self.cur.start_write(writer, &self.buf, cx)
}
pub fn write_remaining<W>(&mut self, writer: &mut W, cx: &mut Context<'_>) -> backend::PollEncodeStatus<io::Error>
where
W: io::AsyncWrite + Unpin,
{
self.cur.write_remaining(writer, &self.buf, cx)
}
pub fn start_read<R>(&mut self, reader: &mut R, cx: &mut Context<'_>) -> backend::PollDecodeStatus<(), io::Error>
where
R: io::AsyncRead + Unpin,
{
self.cur.start_read(reader, &mut self.buf, cx)
}
pub fn read_remaining<R>(&mut self, reader: &mut R, cx: &mut Context<'_>) -> backend::PollDecodeStatus<(), io::Error>
where
R: io::AsyncRead + Unpin,
{
self.cur.read_remaining(reader, &mut self.buf, cx)
}
}