fastly 0.6.0-beta1

Fastly Compute@Edge API
Documentation
pub(crate) mod handle;

use self::handle::StreamingBodyHandle;
use super::Body;
use std::io::{BufWriter, Write};

/// A streaming HTTP body that can be written to, or appended to from another body.
///
/// The interface to this type is very similar to `Body`, however it is write-only, and can only be
/// created as a result of calling
/// [`ResponseExt::stream_to_client()`][downstream-streaming] or
/// [`RequestExt::send_async_streaming()`][async-streaming].
///
/// The most efficient way to write the body is through the [`Write`][write] implementation. Writes are
/// buffered, but calling `Write::flush()` will flush the buffer and cause a new chunk to be written
/// to the downstream client.
///
/// A streaming body will be automatically closed when it goes out of scope, or when it is passed to `drop()`.
///
/// [write]: https://doc.rust-lang.org/std/io/trait.Write.html
/// [downstream-streaming]: ../response/trait.ResponseExt.html#method.stream_to_client
/// [async-streaming]: ../request/trait.RequestExt.html#method.send_async_streaming
pub struct StreamingBody {
    writer: BufWriter<StreamingBodyHandle>,
}

impl StreamingBody {
    // this is not exported, since misuse can lead to data getting dropped or appearing out of order
    fn handle(&mut self) -> &mut StreamingBodyHandle {
        self.writer.get_mut()
    }

    /// Append a body onto the end of this streaming body.
    pub fn append(&mut self, other: Body) {
        // flush the write buffer of the destination body, so that we can use the append method on
        // the underlying handles
        self.writer.flush().expect("fastly_http_body::write failed");
        self.handle().append(other.into_handle())
    }

    /// Write a slice of bytes to the end of this [`StreamingBody`][body].
    ///
    /// This function returns the number of bytes written.
    ///
    /// ```rust,no_run
    /// # let resp = fastly::Response::new();
    /// let mut streaming_body = resp.stream_to_client();
    /// streaming_body.write_bytes(&[0, 1, 2, 3]);
    /// ```
    ///
    /// [body]: struct.StreamingBody.html
    pub fn write_bytes(&mut self, bytes: &[u8]) -> usize {
        self.writer
            .write(bytes)
            .expect("fastly_http_body::write failed")
    }

    /// Write a string slice to the end of this [`StreamingBody`][body].
    ///
    /// This function returns the number of bytes written.
    ///
    /// ```rust,no_run
    /// # let resp = fastly::Response::new();
    /// let mut streaming_body = resp.stream_to_client();
    /// streaming_body.write_str("woof woof");
    /// ```
    ///
    /// [body]: struct.StreamingBody.html
    pub fn write_str(&mut self, string: &str) -> usize {
        self.write_bytes(string.as_ref())
    }
}

impl From<StreamingBodyHandle> for StreamingBody {
    fn from(handle: StreamingBodyHandle) -> Self {
        Self {
            writer: BufWriter::new(handle),
        }
    }
}

// This trait implementation is much simpler than those of `Body`, since we don't have to manage
// multiple buffers. It's just a passthrough to the methods defined on `BufWriter`.
impl Write for StreamingBody {
    fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
        self.writer.write(buf)
    }

    fn write_vectored(&mut self, bufs: &[std::io::IoSlice<'_>]) -> std::io::Result<usize> {
        self.writer.write_vectored(bufs)
    }

    fn flush(&mut self) -> std::io::Result<()> {
        self.writer.flush()
    }
}