Skip to main content

Writer

Struct Writer 

Source
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

Source

pub fn new() -> Self

A writer of a whole document: the header first.

Source

pub fn null(&mut self) -> &mut Self

Null.

Source

pub fn bool(&mut self, b: bool) -> &mut Self

A bool.

Source

pub fn int(&mut self, n: i64) -> &mut Self

An integer: one byte for -16 to 111, else a tag and a varint.

Source

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.

Source

pub fn float32(&mut self, f: f32) -> &mut Self

A 32-bit float: half the bytes of Writer::float.

Source

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).

Source

pub fn bytes(&mut self, b: &[u8]) -> &mut Self

Bytes as they are.

Source

pub fn list(&mut self, len: usize) -> &mut Self

A list of len values.

Source

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

pub fn key(&mut self, k: &str) -> &mut Self

A map’s key.

Source

pub fn finish(self) -> Vec<u8> ⓘ

The bytes.

Source§

impl Writer

Source

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.

Source

pub fn floats32(&mut self, items: &[f32]) -> &mut Self

A list of 32-bit floats, packed: four bytes each, no tag.

Source

pub fn ints(&mut self, items: impl ExactSizeIterator<Item = i64>) -> &mut Self

A list of ints, packed (no tag each).

Source§

impl Writer

Source

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.

Source

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.

Source§

impl Writer

Source

pub fn value(&mut self, v: &Value) -> &mut Self

A whole value. A list of ints or of floats is packed, a list of maps with one set of keys writes the keys once, a map’s keys are a shape, a small int takes a byte.

Trait Implementations§

Source§

impl Debug for Writer

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Writer

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

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.