Module primitives

Module primitives 

Source
Expand description

Codec implementations for Rust primitive types.

§Fixed-size vs Variable-size

Most primitives therefore have a compile-time constant SIZE and can be encoded/decoded without any configuration.

usize is the lone exception: since most values refer to a length or size of an object in memory, values are biased towards smaller values. Therefore, it uses variable-length (varint) encoding to save space. This means that it does not implement FixedSize. When decoding a usize, callers must supply a RangeCfg to bound the allowable value — this protects against denial-of-service attacks that would allocate oversized buffers.

§Safety & portability

  • usize is restricted to values that fit in a u32 to keep the on-wire format identical across 32-bit and 64-bit architectures.
  • All fixed-size integers and floats are written big-endian to avoid host- endian ambiguity.