Skip to main content

ObjectBody

Struct ObjectBody 

Source
pub struct ObjectBody<'a> { /* private fields */ }
Expand description

A JSON object body being written key by key. Created by ResponseBody::object; call finish to close it.

An entry that fails — or that entry_try_with declines — rewinds the buffer past the opening brace, so a partly written object is never left behind for a caller that recovers from it.

Those are the only two exits that rewind. There is no Drop impl, so abandoning an object after a successful entry — an early return, a panic — leaves the open brace and its entries in the buffer. Generated code never does that: every listing either runs to finish or leaves through an arm that has already rewound. A hand-written listing owes the same.

Implementations§

Source§

impl ObjectBody<'_>

Source

pub fn entry<T>(&mut self, key: &str, value: &T)
where T: ServableWrite + ?Sized,

Write one key: value entry.

Source

pub fn entry_with<F>(&mut self, key: &str, f: F) -> StructResult<()>
where F: FnOnce(&mut ResponseBody<'_>) -> StructResult<()>,

Write one key: <value> entry whose value is produced by f, for a nested RepeStruct that fills the body itself.

The body handed to f is marked as nested: the frame’s format is settled by the enclosing object, so nothing inside can switch it. A ResponseBody::write_typed_slice there writes a BEVE typed array inside a BEVE listing and a JSON array inside a JSON one.

Source

pub fn entry_try_with<F>(&mut self, key: &str, f: F) -> Option<StructResult<()>>
where F: FnOnce(&mut ResponseBody<'_>) -> Option<StructResult<()>>,

entry_with for a value the source may decline to produce, as RepeStruct::repe_shared_into declines a path that needs exclusive access.

None rewinds the whole object, exactly as an Err does, so a caller that declines in turn leaves the buffer as it found it without a second step. That is also why a declared count can never be short: an object that did not write every entry does not survive at all.

Source

pub fn finish(self)

Close the object.

§Panics

If the number of entries written differs from the number declared to ResponseBody::object. Under BEVE the count is already in the header, so a disagreement is a corrupt document, and a panic in a handler is caught and reported by the server where corrupt bytes would not be.

Auto Trait Implementations§

§

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

§

impl<'a> Freeze for ObjectBody<'a>

§

impl<'a> RefUnwindSafe for ObjectBody<'a>

§

impl<'a> Send for ObjectBody<'a>

§

impl<'a> Sync for ObjectBody<'a>

§

impl<'a> Unpin for ObjectBody<'a>

§

impl<'a> UnsafeUnpin for ObjectBody<'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, !>

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.