Struct BodyWriter

Source
pub struct BodyWriter<'a> { /* private fields */ }
Expand description

A writer that writes to a Unit shared memory response buffer.

This object is created using Request::write_chunks() or Request::send_chunks_with_writer().

A chunk will be immediately sent to the client once the writer’s memory buffer reaches chunk_size, or flush() is called on the writer, and a new shared memory buffer will be allocated.

The writer will also flush when dropped, but any errors that happen during a drop will panic.

Implementations§

Source§

impl<'a> BodyWriter<'a>

Source

pub fn copy_from_reader<R: Read>(&mut self, r: R) -> Result<()>

Copy from a reader to this writer without using an intermediary buffer.

Normally the Write trait receives an input buffer to copy from, and the ResponseWriter writer will copy from it into Unit’s shared memory.

This method will instead give Unit’s shared memory buffer directly to the Read trait in order to skip copying to a third temporary buffer (such as when using std::io::copy).

Examples found in repository?
examples/request_info.rs (line 40)
16fn request_handler(req: Request) -> UnitResult<()> {
17    // Create and send a response.
18    let headers = &[("Content-Type", "text/plain")];
19    req.send_response(200, headers, "Hello world!\n")?;
20
21    // NGINX Unit uses "Transfer-Encoding: chunked" by default, and can send
22    // additional chunks after the initial response was already sent to the
23    // client.
24    req.send_chunks_with_writer(4096, |w| {
25        write!(w, "Request data:\n")?;
26        write!(w, "  Method: {}\n", req.method())?;
27        write!(w, "  Protocol: {}\n", req.version())?;
28        write!(w, "  Remote addr: {}\n", req.remote())?;
29        write!(w, "  Local addr: {}\n", req.local())?;
30        write!(w, "  Server name: {}\n", req.server_name())?;
31        write!(w, "  Target: {}\n", req.target())?;
32        write!(w, "  Path: {}\n", req.path())?;
33        write!(w, "  Query: {}\n", req.query())?;
34        write!(w, "  Fields:\n")?;
35        for (name, value) in req.fields() {
36            write!(w, "    {}: {}\n", name, value).unwrap();
37        }
38        write!(w, "  Body:\n    ").unwrap();
39
40        w.copy_from_reader(req.body())?;
41
42        Ok(())
43    })?;
44
45    Ok(())
46}

Trait Implementations§

Source§

impl Drop for BodyWriter<'_>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl Write for BodyWriter<'_>

Source§

fn write(&mut self, buf: &[u8]) -> Result<usize>

Writes a buffer into this writer, returning how many bytes were written. Read more
Source§

fn flush(&mut self) -> Result<()>

Flushes this output stream, ensuring that all intermediately buffered contents reach their destination. Read more
1.36.0 · Source§

fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> Result<usize, Error>

Like write, except that it writes from a slice of buffers. Read more
Source§

fn is_write_vectored(&self) -> bool

🔬This is a nightly-only experimental API. (can_vector)
Determines if this Writer has an efficient write_vectored implementation. Read more
1.0.0 · Source§

fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>

Attempts to write an entire buffer into this writer. Read more
Source§

fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>

🔬This is a nightly-only experimental API. (write_all_vectored)
Attempts to write multiple buffers into this writer. Read more
1.0.0 · Source§

fn write_fmt(&mut self, fmt: Arguments<'_>) -> Result<(), Error>

Writes a formatted string into this writer, returning any error encountered. Read more
1.0.0 · Source§

fn by_ref(&mut self) -> &mut Self
where Self: Sized,

Creates a “by reference” adapter for this instance of Write. Read more
Source§

impl UnwindSafe for BodyWriter<'_>

Auto Trait Implementations§

§

impl<'a> Freeze for BodyWriter<'a>

§

impl<'a> RefUnwindSafe for BodyWriter<'a>

§

impl<'a> !Send for BodyWriter<'a>

§

impl<'a> !Sync for BodyWriter<'a>

§

impl<'a> Unpin for BodyWriter<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.