Skip to main content

Module codec

Module codec 

Source
Expand description

A compact, checksummed byte encoding of profiles and timelines, without allocation.

For downlink, persistence across a reset, or handing a timeline to another processor, a LevelProfile, a Timeline and a StateTimeline can be written into a caller’s byte buffer and read back into any storage. The format is fixed-size per element and little-endian:

FieldSizeContent
magic4RTPF (profile), RTTL (timeline) or RTST (state timeline)
version1VERSION
flags1zero, reserved
bodyvariesthe fields below
checksum4CRC-32 (IEEE) of every byte before it
  • profile body: the breakpoint count n (u32), then n breakpoints of at, level, slope (i64 each);
  • timeline body: kind (u8, 0 depletable, 1 claimable), start, initial, min, max (i64), budget (u64), impact count and target count (u32), then each impact as activity (u32), tag (u8: 0 step, 1 claim, 2 rate) and three i64 (at, delta, 0 for a step, start, end, amount or start, end, per_tick otherwise), then each target as at, min;
  • state timeline body: start (i64), initial (u32), budget (u64), impact count (u32), then each impact as activity (u32), tag (u8: 0 set, 1 require), two i64 (at, 0 or start, end) and the value (u32).

decode_in reads into any storage; decode is its heap shorthand (feature alloc), so that Timeline::decode(bytes) needs no type annotation.

Decoding checks the magic, version, length and checksum before it looks at the body, then rebuilds the value through the normal, validating constructors, so a decoded timeline obeys every invariant and capacity of its storage. Insertion order is preserved.

§Examples

use resource_timeline::storage::Fixed;
use resource_timeline::{ActivityId, Bounds, Impact, Target, Timeline};

let mut battery = Timeline::<Fixed<8>>::depletable_in(0, 50, Bounds::new(10, 100)?, 8)?;
battery.add(ActivityId(1), Impact::Rate { start: 0, end: 20, per_tick: -2 })?;
battery.add_target(Target { at: 30, min: 20 })?;

let mut bytes = [0_u8; 256];
let written = battery.encode(&mut bytes)?;
assert_eq!(written, battery.encoded_len());
let restored = Timeline::<Fixed<8>>::decode_in(&bytes[..written])?;
assert_eq!(restored, battery);

// A flipped bit is caught by the checksum.
bytes[20] ^= 1;
assert!(Timeline::<Fixed<8>>::decode_in(&bytes[..written]).is_err());

Constants§

VERSION
The format version written and accepted.

Functions§

crc32
The CRC-32 (IEEE) of bytes, as used by zlib and Ethernet.