Skip to main content

substrate_data/wire/binary/write/
writer_packed.rs

1//! [`Writer`] writing packed lists of numbers: no tag for each.
2
3use super::Writer;
4use crate::binary::{tag, varint, zigzag};
5
6impl Writer {
7    /// A list of floats, packed, no tag each: four bytes each when every
8    /// one is an `f32` exactly ([`tag::FLOATS32`]), else eight.
9    pub fn floats(&mut self, items: &[f64]) -> &mut Self {
10        let narrow = crate::frame::floats::fits_f32(items);
11        self.out
12            .push(if narrow { tag::FLOATS32 } else { tag::FLOATS });
13        varint(&mut self.out, items.len() as u64);
14        if narrow {
15            crate::frame::floats::put_narrow(&mut self.out, items);
16        } else {
17            crate::frame::floats::put_wide(&mut self.out, items);
18        }
19        self
20    }
21
22    /// A list of 32-bit floats, packed: four bytes each, no tag.
23    pub fn floats32(&mut self, items: &[f32]) -> &mut Self {
24        self.out.push(tag::FLOATS32);
25        varint(&mut self.out, items.len() as u64);
26        crate::frame::floats::put_f32s(&mut self.out, items);
27        self
28    }
29
30    /// A list of ints, packed (no tag each).
31    pub fn ints(&mut self, items: impl ExactSizeIterator<Item = i64>) -> &mut Self {
32        self.out.push(tag::INTS);
33        varint(&mut self.out, items.len() as u64);
34        for n in items {
35            varint(&mut self.out, zigzag(n));
36        }
37        self
38    }
39}