Encode a single snapshot chunk into the framed wire format. The CRC-32C is computed over the two `engine_id` bytes followed by all `payload` bytes, then the result is prefixed with the fixed header.
(engine_id: SnapshotEngineId, payload: &[u8])
| 115 | /// The CRC-32C is computed over the two `engine_id` bytes followed by |
| 116 | /// all `payload` bytes, then the result is prefixed with the fixed header. |
| 117 | pub fn encode_snapshot_chunk(engine_id: SnapshotEngineId, payload: &[u8]) -> Vec<u8> { |
| 118 | let engine_bytes = (engine_id as u16).to_be_bytes(); |
| 119 | let crc = { |
| 120 | let mut h = crc32c::crc32c(&engine_bytes); |
| 121 | h = crc32c::crc32c_append(h, payload); |
| 122 | h |
| 123 | }; |
| 124 | |
| 125 | let mut out = Vec::with_capacity(HEADER_LEN + payload.len()); |
| 126 | out.extend_from_slice(&SNAPSHOT_MAGIC); |
| 127 | out.extend_from_slice(&SNAPSHOT_FORMAT_VERSION.to_be_bytes()); |
| 128 | out.extend_from_slice(&engine_bytes); |
| 129 | out.extend_from_slice(&crc.to_be_bytes()); |
| 130 | out.extend_from_slice(payload); |
| 131 | out |
| 132 | } |
| 133 | |
| 134 | /// Decode a framed snapshot chunk, returning the engine id and a slice |
| 135 | /// into the original buffer pointing at the payload (zero-copy). |