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)
| 112 | /// The `kind` and `parent` fields are appended after the original layout, |
| 113 | /// making v1 data readable with backward-compatible defaults (Shared, no parent). |
| 114 | pub fn serialize_view_state(state: &ViewState) -> Vec<u8> { |
| 115 | let mut bytes = Vec::with_capacity(8 + 4 + state.name.len() + 32 + 8 + 1 + 8); |
| 116 | // id: u64 |
| 117 | bytes.extend_from_slice(&state.id.to_le_bytes()); |
| 118 | // name length: u32 |
| 119 | bytes.extend_from_slice(&(state.name.len() as u32).to_le_bytes()); |
| 120 | // name bytes |
| 121 | bytes.extend_from_slice(state.name.as_bytes()); |
| 122 | // merkle state: [u8; 32] |
| 123 | bytes.extend_from_slice(state.state.as_bytes()); |
| 124 | // change_count: u64 |
| 125 | bytes.extend_from_slice(&state.change_count.to_le_bytes()); |
| 126 | // kind: u8 (v2) |
| 127 | bytes.push(state.kind as u8); |
| 128 | // parent: u64 (v2) — NO_PARENT sentinel for None |
| 129 | let parent_val = state.parent.unwrap_or(NO_PARENT); |
| 130 | bytes.extend_from_slice(&parent_val.to_le_bytes()); |
| 131 | bytes |
| 132 | } |
| 133 | |
| 134 | /// Deserialize bytes to a ViewState |
| 135 | /// |