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)
| 60 | /// The `kind` and `parent` fields are appended after the original layout, |
| 61 | /// making v1 data readable with backward-compatible defaults (Shared, no parent). |
| 62 | pub fn serialize_view_state(state: &ViewState) -> Vec<u8> { |
| 63 | let mut bytes = Vec::with_capacity(8 + 4 + state.name.len() + 32 + 8 + 1 + 8); |
| 64 | // id: u64 |
| 65 | bytes.extend_from_slice(&state.id.to_le_bytes()); |
| 66 | // name length: u32 |
| 67 | bytes.extend_from_slice(&(state.name.len() as u32).to_le_bytes()); |
| 68 | // name bytes |
| 69 | bytes.extend_from_slice(state.name.as_bytes()); |
| 70 | // merkle state: [u8; 32] |
| 71 | bytes.extend_from_slice(state.state.as_bytes()); |
| 72 | // change_count: u64 |
| 73 | bytes.extend_from_slice(&state.change_count.to_le_bytes()); |
| 74 | // kind: u8 (v2) |
| 75 | bytes.push(state.kind as u8); |
| 76 | // parent: u64 (v2) — NO_PARENT sentinel for None |
| 77 | let parent_val = state.parent.unwrap_or(NO_PARENT); |
| 78 | bytes.extend_from_slice(&parent_val.to_le_bytes()); |
| 79 | bytes |
| 80 | } |
| 81 | |
| 82 | /// Deserialize bytes to a ViewState |
| 83 | /// |