[Feat][Rust] Add native structural_map and structural_mutate - #704
Open
tlopex wants to merge 4 commits into
Open
[Feat][Rust] Add native structural_map and structural_mutate#704tlopex wants to merge 4 commits into
tlopex wants to merge 4 commits into
Conversation
tlopex
marked this pull request as draft
August 7, 2026 06:27
tlopex
force-pushed
the
feat/rust-structural-map
branch
2 times, most recently
from
August 10, 2026 18:41
5065ebf to
3b06bc9
Compare
Signed-off-by: tlopex <820958424@qq.com>
tlopex
force-pushed
the
feat/rust-structural-map
branch
from
August 10, 2026 19:08
3b06bc9 to
f6358ad
Compare
tlopex
marked this pull request as ready for review
August 11, 2026 21:32
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR adds Rust-native structural transformation support as a counterpart to the structural mapping APIs proposed in #649, building on the Rust
structural_visitandstructural_walkinfrastructure introduced in #693.The structural transformation algorithm remains in Rust: traversal order, recursion, typed dispatch, definition-region propagation, memoization, and identity remapping are all controlled by the Rust implementation.
For runtime-owned Map and Dict storage, this PR introduces a narrow, pointer-only C ABI. The runtime performs only storage-specific operations such as iteration, uniqueness checks, shallow copying, and value replacement. Each child value is synchronously returned to Rust for structural recursion and dispatch.
Rust APIs
This PR provides:
structural_mapwith pre-order and post-order callbacks.StructuralMutatortrait for user-controlled recursion.#[dispatch(map)].Ownership and mutation semantics
The root is consumed so Rust ownership and the runtime strong-reference count
determine whether a container may be reused.
root.clone(), selects copy-on-write behavior.is found.
a stable snapshot if a callback re-enters and mutates an alias of the source
Dict.
completed prefix replacements are retained.
on error paths.
RAII cleanup has completed.
Stable Map and Dict runtime boundary
This PR adds two additive C ABI functions:
TVMFFIMapIterateTVMFFIMapMutateValuesThese functions keep the private
MapBaseObjsmall/dense hash-table layout, iteration links, allocator, and deleter inside the runtime that owns them.Rust no longer mirrors or reads the private C++ Map/Dict storage layout.
TVMFFIAnyvalues cross this boundary only through pointers; no C++AnyorAnyViewclass is passed or returned by value.The callbacks are synchronous, serial, run on the calling thread, and are not retained by the runtime. Structural recursion and nested transformation remain in Rust.
No object layout or existing C ABI is changed. The two new C symbols are additive. Because Rust calls these symbols directly, the Rust crate and runtime must come from compatible versions; the previous old-runtime reconstruction fallback is removed.
Runtime-registered mutation hooks
Following the boundary established by the Rust structural-visit implementation in #693, this PR does not invoke runtime-registered
__s_mutate__or__s_maybe_inplace_mutate__hooks.A non-container type defining either hook is rejected with an actionable error instead of silently replacing its custom mutation semantics with reflected traversal. Its behavior can be implemented explicitly through the Rust
StructuralMutatortrait.This PR does not construct or mirror the C++
StructuralMutatorObjABI. Supporting runtime-registered mutation hooks and allowing those hooks to re-enter a Rust mutator requires a separate interoperability layer and remainsoutside this PR's scope.