Skip to content

[Feat][Rust] Add native structural_map and structural_mutate - #704

Draft
tlopex wants to merge 6 commits into
apache:mainfrom
tlopex:feat/rust-structural-map
Draft

[Feat][Rust] Add native structural_map and structural_mutate#704
tlopex wants to merge 6 commits into
apache:mainfrom
tlopex:feat/rust-structural-map

Conversation

@tlopex

@tlopex tlopex commented Aug 7, 2026

Copy link
Copy Markdown
Member

This PR adds Rust-native structural transformation support, building on the Rust structural_visit and structural_walk infrastructure introduced in #693.

The transformation engine is implemented in Rust.

It provides:

  • structural_map with pre-order and post-order callbacks.
  • A low-level StructuralMutator API for user-controlled recursion.
  • Typed closures, ordered callback tuples, and #[dispatch(map)].
  • Definition-region propagation.
  • DAG and FreeVar identity remapping.
  • Structural error context.
  • Native Array, List, Map, Dict, and reflected-object mutation.
  • Copy-on-write and optional in-place container reuse.

Map and Dict keys remain structural anchors and are not mapped.

Ownership and mutation semantics

The root is consumed so Rust ownership and the runtime strong-reference count define whether a container may be reused.

  • Uniquely owned Array, List, Map, and Dict objects may be updated in place.
  • Passing a shared root, such as root.clone(), selects copy-on-write behavior.
  • A shared immutable Map is copied lazily when its first changed value is found.
  • A shared mutable Dict is copied before the first callback, providing a stable snapshot if a callback re-enters and mutates an alias of the source Dict.
  • If no structural value changes, the original object identity is returned.

Foreign __s_mutate__ hooks are rejected rather than silently replacing custom runtime semantics. Types with custom mutation behavior can explicitly take over through the Rust StructuralMutator API.

Runtime support

This PR adds minimal runtime support for shallow-copying Map and Dict storage.

The runtime helpers preserve:

  • The exact Map or Dict runtime type.
  • Iteration order.
  • Key and value object identity.
  • The runtime allocator and deleter contract.

No object layout or existing C ABI is changed.

The helpers are optional from the Rust crate's perspective. When linked against an older runtime, Rust falls back to the canonical Map/Dict constructors and re-hashes the entries.

tlopex added 4 commits August 6, 2026 23:06
Signed-off-by: tlopex <820958424@qq.com>
Signed-off-by: tlopex <820958424@qq.com>
Signed-off-by: tlopex <820958424@qq.com>
Signed-off-by: tlopex <820958424@qq.com>
@tlopex
tlopex marked this pull request as draft August 7, 2026 06:27
tlopex added 2 commits August 7, 2026 03:03
Signed-off-by: tlopex <820958424@qq.com>
Signed-off-by: tlopex <820958424@qq.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant