| Crates.io | state-tree |
| lib.rs | state-tree |
| version | 3.1.2 |
| created_at | 2025-10-07 02:25:50.321632+00 |
| updated_at | 2025-11-29 01:40:02.424282+00 |
| description | Utility for keeping internal states of signal between 2 compiled source codes |
| homepage | |
| repository | https://github.com/mimium-org/mimium-rs/ |
| max_upload_size | |
| id | 1871143 |
| size | 38,909 |
A state tree utility library that provides functionality for preserving and managing internal DSP (Digital Signal Processing) state between two different compiled source codes.
In mimium, to implement hot-reload functionality, it's necessary to transfer the state of compiled code (delay line buffers, memory values, feedback values, etc.) to newly compiled code. This crate provides a mechanism for efficiently migrating data from an old state tree to a new state tree.
tree module)StateTree - A data structure for hierarchically representing DSP internal state
StateTreeSkeleton - Type-safe representation of state tree "structure"
T: SizedType specifies how to calculate size for each nodetree_diff module)take_diff - Detects differences between old and new trees
Diff detection algorithm:
Detection results are returned as a list of CopyFromPatch
patch module)CopyFromPatch - Instructions for copying data from old tree to new tree
old_path: Path (array of indices) indicating node position in the old treenew_path: Path (array of indices) indicating node position in the new treeapply_patches - Applies patches to transfer old data to the new tree
lib.rs)update_state_storage - Integrated update function
pub fn update_state_storage<T: SizedType + PartialEq>(
old: &[u64], // Flattened binary representation of old state
old_state_skeleton: StateTreeSkeleton<T>, // Old structure
new_state_skeleton: StateTreeSkeleton<T>, // New structure
) -> Result<Option<Vec<u64>>, Box<dyn std::error::Error>>
Processing flow:
take_diff)apply_patches)Returns None if the structure hasn't changed (optimization)
use state_tree::{update_state_storage, tree::*};
// Hold old state in binary
let old_state_bytes: Vec<u64> = vec![/* ... */];
// Structure obtained from old compilation result
let old_skeleton = StateTreeSkeleton::FnCall(vec![
Box::new(StateTreeSkeleton::Delay { len: 2 }),
Box::new(StateTreeSkeleton::Mem(SizeInfo { size: 100 })),
]);
// Structure of newly compiled code
let new_skeleton = StateTreeSkeleton::FnCall(vec![
Box::new(StateTreeSkeleton::Mem(SizeInfo { size: 100 })), // Order changed
Box::new(StateTreeSkeleton::Delay { len: 2 }),
Box::new(StateTreeSkeleton::Feed(SizeInfo { size: 1 })), // Newly added
]);
// Adapt state to new structure
match update_state_storage(&old_state_bytes, &old_skeleton, &new_skeleton) {
Ok(Some(new_state_bytes)) => {
// Use state adapted to new structure
}
Ok(None) => {
// Structure hasn't changed
}
Err(e) => {
// Error handling
}
}
serialize_tree_untagged: Flattens StateTree to Vec<u64>
deserialize_tree_untagged: Restores Vec<u64> to StateTree using skeleton
By using the LCS algorithm:
Access to each node is specified by path (array of indices):
old_path: [2, 1, 0] // Child [2] → its child [1] → its child [0]
tests/diff.rs)tests/end2end.rs)SizedType trait generalizes size calculation for each nodeResult on deserialization failureFollows the common license of the workspace Ï