pub fn structural_mutate<R, H, M, D, O>(root: R, dispatcher: H) -> Result<Any>where
R: Into<Any>,
O: BorrowMut<D>,
for<'v, 'c, 'r> H: IntoDispatcher<(MutationView<'v>, &'c mut StructuralMutatorView<'r, D, StructuralMutatorObj>), Option<Result<UnchangedOr<Any>>>, M, Target = D, Output = O>,
for<'s, 'v, 'c, 'r> &'s mut D: Dispatcher<(MutationView<'v>, &'c mut StructuralMutatorView<'r, D, StructuralMutatorObj>), Option<Result<UnchangedOr<Any>>>>,Expand description
Mutate a structured value with an owned or borrowed Dispatcher.
The root is consumed to establish the ownership boundary for optional in-place mutation. A matching callback supplies the final value; unmatched values use default mutation.
Callbacks must report failures through Result and must not panic: a panic
reaching the non-unwinding native callback boundary aborts the process.
A callback must not modify an ffi.List or ffi.Dict that holds the
value it receives or one of that value’s ancestors: the traversal borrows
their cells while the callback runs. A callback that returns an Error
object as its value fails the traversal with a TypeError.
§Arguments
root- Owned value to mutate, convertible into anAny. A uniquely owned container may be reused in place; clone first to preserve the original.dispatcher- Owned or mutably borrowed dispatcher implementingIntoDispatcher. Matching handlers return replacements and control recursion throughStructuralMutatorView; unmatched values use default mutation. For closure callbacks with automatic recursion, usestructural_map.
§Examples
Double the integers in nested arrays; default mutation descends into the arrays:
use tvm_ffi::{dispatch, structural_mutate, Any, Array, MutationView, Result, UnchangedOr};
use tvm_ffi::StructuralMutatorView;
struct Double;
#[dispatch(mutate, (MutationView<'_>, &mut StructuralMutatorView<'_, Self>), Result<UnchangedOr<Any>>)]
impl Double {
fn mutate_int(
&mut self,
value: i64,
_: &mut StructuralMutatorView<'_, Self>,
) -> Result<UnchangedOr<Any>> {
Ok((value * 2).into())
}
}
let root = Array::from_vec(vec![Array::from_vec(vec![1_i64, 2]), Array::from_vec(vec![3_i64])]);
let result = Array::<Array<i64>>::try_from(structural_mutate(root, Double)?)?;
assert_eq!(result[0].iter().copied().collect::<Vec<_>>(), vec![2, 4]);