Skip to main content

encode_frame

Function encode_frame 

Source
pub fn encode_frame(
    document: &str,
    out: &mut Vec<u8>,
    limit: MaxFrameSize,
) -> Result<(), FramingError>
Expand description

Appends one frame — the document, then the terminator — to out.

The write half of the same framing, so a server and a client cannot disagree about where a frame ends. out is taken rather than returned so a transport writing into a reused buffer pays no allocation per frame.

The document is neither validated nor escaped: it is a document some codec already produced, and a frame is a container, not a parser. What is checked is the size, because a frame this process emits has to satisfy the same ceiling a peer’s does, and learning that at the write is far better than learning it as a truncated read on the other side.

§Errors

FramingError::FrameTooLarge when document is longer than limit. The limit counts the document’s bytes and not the terminator, so a document of exactly limit bytes is a legal frame.

§Examples

use protocol_droid::domain::{MaxFrameSize, encode_frame};

let mut wire = Vec::new();
encode_frame("{\"jsonrpc\":\"2.0\"}", &mut wire, MaxFrameSize::new(64)?)?;

assert_eq!(wire, b"{\"jsonrpc\":\"2.0\"}\n");