Skip to content

[Rust][Feat] Add match_any! for object-backed values#682

Merged
tqchen merged 13 commits into
apache:mainfrom
tlopex:feat/rust-match-any
Jul 23, 2026
Merged

[Rust][Feat] Add match_any! for object-backed values#682
tqchen merged 13 commits into
apache:mainfrom
tlopex:feat/rust-match-any

Conversation

@tlopex

@tlopex tlopex commented Jul 22, 2026

Copy link
Copy Markdown
Member

This PR adds Rust match_any!, a Phase 1 macro that evaluates an Any-compatible scrutinee once, converts it to AnyView, and tries object-backed target conversions in source order using the existing standard TryInto/TryFrom implementations. The first successful conversion whose optional guard passes selects its arm; a failed conversion or guard continues to the next arm, while a required final _ handles non-object or unmatched values. This implementation intentionally does not add type-index dispatch tables, caches, custom pattern protocols, or heuristic dispatch.

match_any! {
    value {
        Tensor(tensor)
            if tensor.shape().len() == 2 => ("matrix", tensor.shape().len()),
        Tensor(tensor) => ("tensor", tensor.shape().len()),
        Shape(shape) => ("shape", shape.len()),
        Array::<i64>(array) => ("array", array.len()),
        _ => ("unsupported", 0),
    }
}

@gemini-code-assist

Copy link
Copy Markdown
Contributor

Caution

The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased.

@tlopex
tlopex marked this pull request as draft July 22, 2026 09:23
@tlopex
tlopex force-pushed the feat/rust-match-any branch from 5c51eae to 8625f3e Compare July 22, 2026 18:39
Comment thread rust/tvm-ffi/src/match_any.rs Outdated
Comment thread rust/tvm-ffi/tests/test_match_any.rs Outdated
Comment thread rust/tvm-ffi/tests/test_match_any.rs Outdated
@tlopex
tlopex marked this pull request as ready for review July 22, 2026 22:48
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Caution

The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased.

@tlopex
tlopex requested a review from Seven-Streams July 22, 2026 22:50
@tlopex
tlopex marked this pull request as draft July 22, 2026 23:02
@tlopex tlopex changed the title [Rust][Feat] Add ordered match_any! dispatch [Rust][Feat] Add ordered match_object! dispatch Jul 22, 2026
@tlopex tlopex changed the title [Rust][Feat] Add ordered match_object! dispatch [Rust][Feat] Add ordered match_any! dispatch Jul 22, 2026
@tlopex
tlopex marked this pull request as ready for review July 23, 2026 01:01
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Caution

The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased.

@tlopex
tlopex requested review from Copilot and removed request for Seven-Streams July 23, 2026 01:01
@tlopex
tlopex requested a review from Seven-Streams July 23, 2026 01:03

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a new Rust match_any! procedural macro to support source-ordered, object-only dispatch over values carried by Any/AnyView/object handles, and wires it into the tvm_ffi public API with initial tests.

Changes:

  • Introduces match_any! macro in tvm-ffi-macros with parsing + expansion for ordered typed arms and required _ fallback.
  • Re-exports the macro from tvm_ffi and adds an AnyView conversion helper to support borrowed AnyView scrutinees.
  • Adds an integration test covering source-order behavior across several object container types.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
rust/tvm-ffi/tests/test_match_any.rs New integration test validating ordered dispatch across common object containers.
rust/tvm-ffi/src/lib.rs Re-exports match_any from tvm_ffi_macros at the crate root.
rust/tvm-ffi/src/any.rs Adds From<&AnyView> for AnyView to support borrowed AnyView scrutinees.
rust/tvm-ffi-macros/src/utils.rs Refines “local crate vs external crate” detection for macro expansion.
rust/tvm-ffi-macros/src/match_any.rs Implements the match_any! proc-macro parser and code generator.
rust/tvm-ffi-macros/src/lib.rs Exposes the new match_any proc macro and documents intended semantics.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread rust/tvm-ffi-macros/src/utils.rs Outdated
Comment thread rust/tvm-ffi/tests/test_match_any.rs
Comment thread rust/tvm-ffi-macros/src/match_any.rs
@tlopex tlopex changed the title [Rust][Feat] Add ordered match_any! dispatch [Rust][Feat] Add match_any! for object-backed values Jul 23, 2026
Comment thread rust/tvm-ffi-macros/src/utils.rs Outdated
@tlopex

tlopex commented Jul 23, 2026

Copy link
Copy Markdown
Member Author

cc @tqchen

@tqchen
tqchen merged commit 135c2e8 into apache:main Jul 23, 2026
9 checks passed
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.

4 participants