Decode `bytes` with a shape whose decode exactly reproduces the input. Decode-then-re-encode equality guarantees complete consumption and a byte-exact layout match, so a truncated or misaligned payload can never be silently reinterpreted as an older shape that drops appended fields (review ATOM::aaron::8 R1). postcard encodings are canonical for these types, so a valid decode of matching layout a
(bytes: &[u8])
| 633 | /// (review ATOM::aaron::8 R1). postcard encodings are canonical for these |
| 634 | /// types, so a valid decode of matching layout always re-encodes identically. |
| 635 | pub(crate) fn decode_exact_shape<T>(bytes: &[u8]) -> Option<T> |
| 636 | where |
| 637 | T: serde::de::DeserializeOwned + Serialize, |
| 638 | { |
| 639 | let value: T = postcard::from_bytes(bytes).ok()?; |
| 640 | let reencoded = postcard::to_allocvec(&value).ok()?; |
| 641 | (reencoded == bytes).then_some(value) |
| 642 | } |
| 643 | |
| 644 | /// Portable, content-addressed root for an Atomic session ledger. |
| 645 | /// |