pub struct Writer { /* private fields */ }Expand description
Writes the binary form piece by piece, from data that isn’t a Value
— nothing is built or copied on the way. A map or list is given its
length, then that many entries (key then a value) or values.
let mut w = substrate_data::binary::Writer::new();
w.map(2).key("id").str("docs/intro").key("count").int(3);
let v = substrate_data::binary::decode(&w.finish()).unwrap();
assert_eq!(v.get("count").and_then(|c| c.as_i64()), Some(3));Implementations§
Source§impl Writer
impl Writer
Sourcepub fn int(&mut self, n: i64) -> &mut Self
pub fn int(&mut self, n: i64) -> &mut Self
An integer: one byte for -16 to 111, else a tag and a varint.
Sourcepub fn float(&mut self, f: f64) -> &mut Self
pub fn float(&mut self, f: f64) -> &mut Self
A float: four bytes when it is an f32 exactly (layout::narrows, what a
reader gives back bit for bit), else eight.
Sourcepub fn float32(&mut self, f: f32) -> &mut Self
pub fn float32(&mut self, f: f32) -> &mut Self
A 32-bit float: half the bytes of Writer::float.
Sourcepub fn str(&mut self, s: &str) -> &mut Self
pub fn str(&mut self, s: &str) -> &mut Self
A string: its bytes the first time, its index after (in the tag itself for the first 32 strings of the document).
Sourcepub fn map(&mut self, len: usize) -> &mut Self
pub fn map(&mut self, len: usize) -> &mut Self
A map of len entries, each a Writer::key and a value: its
keys written one by one, and again in every map that has them.
Writer::record writes a map’s keys once for all maps with them.
Source§impl Writer
impl Writer
Sourcepub fn floats(&mut self, items: &[f64]) -> &mut Self
pub fn floats(&mut self, items: &[f64]) -> &mut Self
A list of floats, packed, no tag each: four bytes each when every
one is an f32 exactly (tag::FLOATS32), else eight.
Sourcepub fn floats32(&mut self, items: &[f32]) -> &mut Self
pub fn floats32(&mut self, items: &[f32]) -> &mut Self
A list of 32-bit floats, packed: four bytes each, no tag.
Sourcepub fn ints(&mut self, items: impl ExactSizeIterator<Item = i64>) -> &mut Self
pub fn ints(&mut self, items: impl ExactSizeIterator<Item = i64>) -> &mut Self
A list of ints, packed (no tag each).
Source§impl Writer
impl Writer
Sourcepub fn record(&mut self, keys: &[&str]) -> &mut Self
pub fn record(&mut self, keys: &[&str]) -> &mut Self
A map of these keys (none repeated): the keys are written the
first time this set is met and named by number after that. A value
for each key follows, in order, with no Writer::key.
Sourcepub fn record_masked(
&mut self,
keys: &'static [&'static str],
present: u64,
) -> &mut Self
pub fn record_masked( &mut self, keys: &'static [&'static str], present: u64, ) -> &mut Self
A map of the keys of keys that present has a bit for (bit i
for key i, at most 64 keys): what Writer::record is, for a
struct whose keys are always the same slice, found again by where it
is instead of by looking at its keys. A value for each key present
follows, in order.