Skip to main content

write_message_streaming

Function write_message_streaming 

Source
pub fn write_message_streaming<W, F, E>(
    w: &mut W,
    header: Header,
    query: &[u8],
    body_len: u64,
    body_writer: F,
) -> Result<(), RepeError>
where W: Write, F: FnOnce(&mut W) -> Result<(), E>, E: Into<RepeError>,
Expand description

Emit a REPE message whose body is produced by body_writer rather than owned by a Message value.

The caller supplies the wire body length (body_len) up front; this lets the header be written before the body and avoids buffering the body into a Vec<u8> first. header.query_length, header.body_length, and header.length are overwritten to reflect query.len() and body_len, so the caller does not need to set them before calling this function.

The body_writer closure must emit exactly body_len bytes; failing to do so produces an unframed message on the wire and the receiving side will either block (short body) or misinterpret subsequent frames (long body).

body_writer’s error type only has to be convertible into RepeError, so a raw w.write_all(..) and a streaming encode such as structio::to_beve_writer — which reports std::io::Error, because a BEVE write cannot fail on content — both work without wrapping, and the failure keeps its original error variant.

Because REPE frames are length-prefixed, body_len has to be known before the body is written. Three patterns supply a serialized body without building a full wire-frame Vec:

  • Reusable scratch buffer (recommended for a writer sending many frames): serialize the body once into a caller-owned Vec<u8> with structio::write_beve_into, which reuses the buffer’s allocation and clears it on each call, then pass scratch.len() as body_len and |w| w.write_all(&scratch) as the writer. One encode per message and, once the buffer has grown to fit the largest body, no further allocation. See examples/beve_streaming_body.rs.

  • Known-length raw body: when the length is already known (a file chunk, a pre-sized buffer), pass it directly and have body_writer emit the bytes with w.write_all(..) — no body buffer at all.

  • Zero-buffer streaming (for a body too large to hold in memory): measure the encoded length with structio::beve_size, then stream the body straight to the sink with structio::to_beve_writer inside body_writer — the body is never materialized as a Vec:

    let body_len = structio::beve_size(value) as u64;
    write_message_streaming(w, header, query, body_len, |w| {
        structio::to_beve_writer(value, w)
    })

The zero-buffer path costs a size pass over the value before the write pass. That pass allocates nothing and moves no bytes (each leaf is an integer add): it is O(1) for a &[u8] blob or a numeric slice, whose encoded length is arithmetic on the element count, but O(payload) for a nested structure, which is walked member by member. Prefer it only when not holding the whole body at once outweighs that second traversal; otherwise the reusable scratch buffer is a single encode.

Error fidelity is no longer a caveat: a structio write cannot fail on content, so the only failure a streaming encode can report is the sink’s own std::io::Error, and it arrives as RepeError::Io exactly as the scratch buffer’s write_all does.