pub fn append_aligned<T: Write + ?Sized>(value: &T, out: &mut Vec<u8>)Expand description
Serialize a value’s aligned form after what a buffer already holds.
to_vec_aligned into a buffer with something in front of it. The padding
that lands each numeric payload on its own element width is measured from
the start of the document, and the document starts where this buffer does,
so a value appended behind a fixed header and a variable-length prefix is
padded against the length of both. That is the whole difference from
to_vec_aligned, which has nothing in front of it and counts from zero.
let samples = vec![1.5f64, 2.5, 3.5];
let mut frame = vec![0u8; 12]; // a header, already written
let body = structio::beve_size_aligned_after(&samples, frame.len());
structio::append_beve_aligned(&samples, &mut frame);
assert_eq!(frame.len(), 12 + body);
assert_eq!(structio::from_beve::<Vec<f64>>(&frame[12..]).unwrap(), samples);size_aligned_after is the matching measurement, as above, and a panic
out of the value’s Write impl leaves out as it was, as in append.
This takes the buffer for the document, which is right for a message
assembled in one buffer and wrong for the two cases where it is not. A body
built on its own to be concatenated behind a header later has nothing in
front of it here; a frame appended to a send buffer that still holds the
frames before it has too much. Neither is a mistake this can detect, and
both come out padded for a document nobody will read as one, so say where
the value stands and use Writer directly:
use structio::Standard;
use structio::beve::{Write, Writer};
let samples = vec![1.5f64, 2.5];
let mut send = vec![0u8; 100]; // frames already queued
let frame_start = send.len();
send.extend_from_slice(&[0u8; 48]); // this frame's header
// Where the body stands in *this frame*, not in the send buffer.
let at = send.len() - frame_start;
let body = structio::beve_size_aligned_after(&samples, at);
let mut w = Writer::<Standard>::appending(send).aligned().at(at);
samples.write(&mut w);
let send = w.into_vec();
assert_eq!(send.len() - frame_start - 48, body);
assert_eq!(structio::from_beve::<Vec<f64>>(&send[frame_start + 48..]).unwrap(), samples);