use std::io::{
self,
Write,
};
pub(in crate::http) struct BoundedBodyWriter {
output: Vec<u8>,
max_bytes: usize,
}
impl BoundedBodyWriter {
#[inline]
pub(in crate::http) const fn new(max_bytes: usize) -> Self {
Self {
output: Vec::new(),
max_bytes,
}
}
#[inline]
pub(in crate::http) fn into_string(self) -> Option<String> {
String::from_utf8(self.output).ok()
}
}
impl Write for BoundedBodyWriter {
fn write(&mut self, buffer: &[u8]) -> io::Result<usize> {
if self.output.len().saturating_add(buffer.len()) > self.max_bytes {
return Err(io::Error::from(io::ErrorKind::WriteZero));
}
self.output.extend_from_slice(buffer);
Ok(buffer.len())
}
#[inline(always)]
fn flush(&mut self) -> io::Result<()> {
Ok(())
}
}