use super::super::handle::BodyHandle;
use crate::abi;
use std::io::Write;
#[derive(Debug, Eq, Hash, PartialEq)]
#[repr(transparent)]
pub struct StreamingBodyHandle {
handle: u32,
}
impl StreamingBodyHandle {
pub(crate) fn from_body_handle(body_handle: &BodyHandle) -> Self {
Self {
handle: body_handle.handle,
}
}
fn as_body_handle(&self) -> BodyHandle {
BodyHandle {
handle: self.handle,
}
}
#[cfg_attr(
feature = "unstable-doc",
doc(include = "../../../../docs/snippets/body-append-constant-time.md")
)]
pub fn append(&mut self, other: BodyHandle) {
self.as_body_handle().append(other)
}
pub fn write_bytes(&mut self, bytes: &[u8]) -> usize {
self.as_body_handle().write_bytes(bytes)
}
pub fn write_str(&mut self, string: &str) -> usize {
self.write_bytes(string.as_bytes())
}
}
impl Drop for StreamingBodyHandle {
fn drop(&mut self) {
unsafe { abi::fastly_http_body::close(self.as_body_handle().as_u32()) }
.result()
.expect("fastly_http_body::close failed");
}
}
impl Write for StreamingBodyHandle {
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
Ok(self.write_bytes(buf))
}
fn flush(&mut self) -> std::io::Result<()> {
Ok(())
}
}