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<'_>
impl ObjectBody<'_>
Sourcepub fn entry<T>(&mut self, key: &str, value: &T)where
T: ServableWrite + ?Sized,
pub fn entry<T>(&mut self, key: &str, value: &T)where
T: ServableWrite + ?Sized,
Write one key: value entry.
Sourcepub fn entry_with<F>(&mut self, key: &str, f: F) -> StructResult<()>
pub fn entry_with<F>(&mut self, key: &str, f: F) -> 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.
Sourcepub fn entry_try_with<F>(&mut self, key: &str, f: F) -> Option<StructResult<()>>
pub fn entry_try_with<F>(&mut self, key: &str, f: F) -> 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.
Sourcepub fn finish(self)
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.