substrate_data/wire/binary/layout.rs
1//! The header and the fixed sizes of the binary form.
2
3/// What a binary file starts with.
4pub const MAGIC: &[u8; 4] = b"SDAT";
5/// The version of the form after the magic that every writer writes.
6pub const VERSION: u8 = 2;
7
8/// The first version, still read and never written: a v1 document is a v2
9/// one that uses only the tags `0..=14` (the reader checks that it does).
10pub const LEGACY_VERSION: u8 = 1;
11
12/// The smallest int a tag of its own holds ([`crate::binary::tag::INT_INLINE`]).
13pub const INLINE_INT_MIN: i64 = -16;
14
15/// The largest int a tag of its own holds.
16pub const INLINE_INT_MAX: i64 = 111;
17
18/// How many table strings a tag of their own can name ([`crate::binary::tag::STR_INDEX`]).
19pub const STR_INDEX_COUNT: u8 = 32;
20
21/// Nested deeper than this is malformed.
22pub const MAX_DEPTH: usize = 256;
23
24/// A stream's length: fixed width, so the writer fills it in after its items.
25pub const STREAM_LEN_BYTES: usize = 4;
26
27/// A string's varint: a new one's (odd, its length) or a reference to the
28/// table (even, its index).
29pub const NEW_STRING: u64 = 1;
30
31/// Whether `f` is an `f32` exactly, bit for bit: what a float of four
32/// bytes holds. A NaN never is (its payload is not worth the question).
33#[must_use]
34pub(crate) fn narrows(f: f64) -> bool {
35 !f.is_nan() && f64::from(narrow(f)).to_bits() == f.to_bits()
36}
37
38/// `f` as an `f32`: only when [`narrows`] says it loses nothing.
39#[expect(
40 clippy::cast_possible_truncation,
41 reason = "called to check, or once `narrows` said it is exact"
42)]
43pub(crate) fn narrow(f: f64) -> f32 {
44 f as f32
45}