pub struct ResponseBody<'a> { /* private fields */ }Expand description
Response body under construction for a RepeStruct read.
Handed to RepeStruct::repe_handle_into so a read serializes the live field
straight into the outgoing frame buffer. The alternative — returning a
serde_json::Value that the router then re-serializes — allocates an
intermediate tree per read, which is what a client polling a wide object at
kilohertz pays for.
The body starts out empty and BodyFormat::Json; the write that fills it
also settles the format, so a write_typed_slice
body reports BodyFormat::Beve. Exactly one top-level write is expected —
a value, a null, or an object.
Any failed write — including one that fails partway through an
object — rewinds the buffer to where that write began, so a
caller that recovers from a StructError never ships half-serialized
bytes.
Implementations§
Source§impl<'a> ResponseBody<'a>
impl<'a> ResponseBody<'a>
Sourcepub fn new(buf: &'a mut Vec<u8>) -> Self
pub fn new(buf: &'a mut Vec<u8>) -> Self
Wrap an output buffer. buf is normally empty; writes append to it.
Sourcepub fn format(&self) -> BodyFormat
pub fn format(&self) -> BodyFormat
The body format settled by the write that filled this body.
Sourcepub fn write<T>(&mut self, path: &str, value: &T) -> StructResult<()>
pub fn write<T>(&mut self, path: &str, value: &T) -> StructResult<()>
Serialize value as the whole body, as JSON.
path names the field for the error message and is only read when
serialization fails.
Sourcepub fn write_null(&mut self)
pub fn write_null(&mut self)
Write a JSON null body — the response to a write, or to a read that has
nothing to return.
Sourcepub fn write_typed_slice<T>(
&mut self,
path: &str,
slice: &[T],
) -> StructResult<()>where
T: BeveTypedSlice + Serialize,
pub fn write_typed_slice<T>(
&mut self,
path: &str,
slice: &[T],
) -> StructResult<()>where
T: BeveTypedSlice + Serialize,
Write slice as a BEVE typed numeric array and set the body format to
BodyFormat::Beve.
The same bulk encode repe::message::MessageBuilder::body_typed_slice
produces — one copy_nonoverlapping on little-endian targets rather than
a per-element serde walk, and byte-identical to what Glaze emits for the
same array. This is what #[repe(typed)] routes a numeric field to.
(Named rather than linked: the builder lives in repe, which this crate
deliberately does not depend on.)
Inside an enclosing object — the whole-struct listing — the frame is already committed to JSON, so the slice is written as a JSON array instead. A field only reaches the typed encoding when it is read on its own.
Requires the typed feature, which is what carries beve. It is on
through repe and off for a direct repe-core dependency, where this
signature is replaced by one whose bound no type satisfies, so that
calling it is a compile error naming the feature.
Sourcepub fn object(&mut self) -> ObjectBody<'_>
pub fn object(&mut self) -> ObjectBody<'_>
Begin a JSON object body, written key by key.
Used by the whole-struct listing so a wide object is emitted in one pass
instead of through a serde_json::Map.