[FEAT][RUST] Add AnyCompatible::to_any. (#712)
## Summary
Add `AnyCompatible::to_any(&self) -> Any`, the by-reference counterpart
of `Any::from(value)`.
`From<AnyView> for Any` now dispatches inline instead of always calling
`TVMFFIAnyViewToOwnedAny`: an object increfs, a self-contained cell is a
bitwise copy, and only the three borrowed forms Rust never produces
(`kTVMFFIRawStr`, `kTVMFFIByteArrayPtr`, `kTVMFFIObjectRValueRef`) still
go to the runtime, behind `#[cold] #[inline(never)]`. This mirrors C++
`details::InplaceConvertAnyViewToAny`. `is_plain_inline` moves from
`extra::structural_common` into `any.rs`.
## Motivation
`Any::from` takes ownership. A value reachable only through a shared
reference — a field behind a `Deref` into object storage, e.g. `node.a`
on a `&AddObj` — cannot be moved out, so every such site has to write
`Any::from(node.a.clone())` today.
`impl From<&T> for Any` cannot be added instead: `&` is fundamental, so
a downstream crate may implement `AnyCompatible` for its own `&T` and
the two impls overlap (E0119). A provided trait method has no such
conflict, and every `AnyCompatible` type — including `Option<T>` and the
containers — picks it up for free.
## Usage
```rust
fn first_operand(node: &AddObj) -> Any {
node.a.to_any() // was: Any::from(node.a.clone())
}
```
`to_any()` leaves the borrowed value usable and retains object-backed
values by increfing them.
## Efficiency and Test
`to_any` costs whatever `From<AnyView> for Any` costs, which no longer
crosses the C ABI on every conversion. Where the type index is a
compile-time constant (`i64`, `Array`, `Map`) the dispatch folds away
and `to_any` matches `Any::from`. For `String`/`Bytes` and derived
object refs the index is read at runtime, so the normalizing branch
survives and the caller keeps a stack frame. That is deliberate: a type
whose view is a borrowed representation — the Rust counterpart of C++
`RValueRef<T>` — needs that branch to stay.
The result is on par with `Any::from`: neither emits a call or an unwind
path, and on the object path both do the same single `lock incq`.
Verified on the release asm (x86-64, `-C codegen-units=1`).
Tests in `tests/test_any.rs`: `to_any` matches `Any::from` across the
scalar, small-string/bytes, and object representations and increfs
exactly once (given back on drop); and it works on fields reached
through `Deref` — `String`, `Option<String>`, `i64` — where `Any::from`
does not compile.
Docs: new "Converting Borrowed Values into `Any`" section in
`docs/guides/rust_lang_guide.md`.
---------
Signed-off-by: yuchuan <yuchuan.7streams@gmail.com>📚 Documentation | 🚀 Quickstart
Apache TVM FFI is an open ABI and FFI for machine learning systems. It is a minimal, framework-agnostic, yet flexible open convention with the following systems in mind:
These enable broad interoperability across frameworks, libraries, DSLs, and agents; the ability to ship one wheel for multiple frameworks and Python versions (including free-threaded Python); and consistent infrastructure across environments.
Install TVM-FFI with pip, uv or from source:
pip install apache-tvm-ffi pip install torch-c-dlpack-ext # compatibility package for torch <= 2.9
C ABI stability is our top priority.
Status: RFC Main features are complete and ABI stable. We recognize potential needs for evolution to ensure it works best for the machine learning systems community, and would like to work together with the community for such evolution. We plan to stay in the RFC stage for three months from the v0.1.0 release.
Releases during the RFC stage will be 0.X.Y, where bumps in X indicate C ABI-breaking changes and Y indicates other changes. We anticipate the RFC stage will last for three months, then we will start following Semantic Versioning (major.minor.patch) going forward.
Our documentation site includes: