state_tree/
lib.rs

1pub mod patch;
2pub mod tree;
3pub mod tree_diff;
4
5use crate::tree::{SizedType, StateTreeSkeleton};
6
7pub fn update_state_storage<T: SizedType + PartialEq>(
8    old: &[u64],
9    old_state_skeleton: StateTreeSkeleton<T>,
10    new_state_skeleton: StateTreeSkeleton<T>,
11) -> Result<Option<Vec<u64>>, Box<dyn std::error::Error>> {
12    if old_state_skeleton == new_state_skeleton {
13        return Ok(None);
14    }
15
16    let totalsize = new_state_skeleton.total_size();
17    let patches = tree_diff::take_diff(&old_state_skeleton, &new_state_skeleton)
18        .into_iter()
19        .collect::<Vec<_>>();
20
21    // 新しいストレージを0で初期化
22    let mut new_storage = vec![0u64; totalsize as usize];
23
24    for patch in &patches {
25        log::debug!("Patch: {patch:?}");
26    }
27
28    // フラットな配列から配列へパッチを適用
29    patch::apply_patches(&mut new_storage, old, patches.as_slice());
30
31    debug_assert_eq!(new_storage.len(), totalsize as usize);
32
33    Ok(Some(new_storage))
34}