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:
| Field | Size | Content |
|---|---|---|
| magic | 4 | RTPF (profile), RTTL (timeline) or RTST (state timeline) |
| version | 1 | VERSION |
| flags | 1 | zero, reserved |
| body | varies | the fields below |
| checksum | 4 | CRC-32 (IEEE) of every byte before it |
- profile body: the breakpoint count
n(u32), thennbreakpoints ofat,level,slope(i64each); - 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 threei64(at, delta, 0for a step,start, end, amountorstart, end, per_tickotherwise), then each target asat, 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), twoi64(at, 0orstart, 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.