Serialize a ViewState to bytes Layout (v2): [id:8][name_len:4][name:var][merkle:32][change_count:8][kind:1][parent:8] The `kind` and `parent` fields are appended after the original layout, making v1 data readable with backward-compatible defaults (Shared, no parent).
(state: &ViewState)
| 79 | /// The `kind` and `parent` fields are appended after the original layout, |
| 80 | /// making v1 data readable with backward-compatible defaults (Shared, no parent). |
| 81 | pub fn serialize_view_state(state: &ViewState) -> Vec<u8> { |
| 82 | let mut bytes = Vec::with_capacity(8 + 4 + state.name.len() + 32 + 8 + 1 + 8); |
| 83 | // id: u64 |
| 84 | bytes.extend_from_slice(&state.id.to_le_bytes()); |
| 85 | // name length: u32 |
| 86 | bytes.extend_from_slice(&(state.name.len() as u32).to_le_bytes()); |
| 87 | // name bytes |
| 88 | bytes.extend_from_slice(state.name.as_bytes()); |
| 89 | // merkle state: [u8; 32] |
| 90 | bytes.extend_from_slice(state.state.as_bytes()); |
| 91 | // change_count: u64 |
| 92 | bytes.extend_from_slice(&state.change_count.to_le_bytes()); |
| 93 | // kind: u8 (v2) |
| 94 | bytes.push(state.kind as u8); |
| 95 | // parent: u64 (v2) — NO_PARENT sentinel for None |
| 96 | let parent_val = state.parent.unwrap_or(NO_PARENT); |
| 97 | bytes.extend_from_slice(&parent_val.to_le_bytes()); |
| 98 | bytes |
| 99 | } |
| 100 | |
| 101 | /// Deserialize bytes to a ViewState |
| 102 | /// |