MCPcopy Create free account
hub / github.com/atomicdotdev/atomic / decode_exact_shape

Function decode_exact_shape

atomic-core/src/change/session.rs:635–642  ·  view source on GitHub ↗

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])

Source from the content-addressed store, hash-verified

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.
635pub(crate) fn decode_exact_shape<T>(bytes: &[u8]) -> Option<T>
636where
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///

Callers

nothing calls this directly

Calls 1

okMethod · 0.45

Tested by

no test coverage detected