Skip to main content

ResponseBody

Struct ResponseBody 

Source
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>

Source

pub fn new(buf: &'a mut Vec<u8>) -> Self

Wrap an output buffer. buf is normally empty; writes append to it.

Source

pub fn format(&self) -> BodyFormat

The body format settled by the write that filled this body.

Source

pub fn write<T>(&mut self, path: &str, value: &T) -> StructResult<()>
where T: Serialize + ?Sized,

Serialize value as the whole body, as JSON.

path names the field for the error message and is only read when serialization fails.

Source

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.

Source

pub fn write_typed_slice<T>( &mut self, path: &str, slice: &[T], ) -> StructResult<()>

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.

Source

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.

Auto Trait Implementations§

§

impl<'a> !UnwindSafe for ResponseBody<'a>

§

impl<'a> Freeze for ResponseBody<'a>

§

impl<'a> RefUnwindSafe for ResponseBody<'a>

§

impl<'a> Send for ResponseBody<'a>

§

impl<'a> Sync for ResponseBody<'a>

§

impl<'a> Unpin for ResponseBody<'a>

§

impl<'a> UnsafeUnpin for ResponseBody<'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 = !

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.