tqchen opened a new pull request, #657: URL: https://github.com/apache/tvm-ffi/pull/657
## Motivation Make `tvm::ffi::Optional<T>` and `tvm::ffi::Variant<...>` layout stable across the contained type `T`, so the ABI does not shift with `T`. This is needed for a stable cross-language ABI for optionals. ## Changes **Variant**: collapse the templated `VariantBase<all_object_ref>` (dropping the all-`ObjectRef` `ObjectRef`-derived specialization and the `all_object_ref_v` switch) into `Variant` holding a single `Any` directly; `GetObjectPtrForHashEqual` reads the object pointer via `AnyUnsafe::ObjectPtrFromAnyAfterCheck`. **Optional**: `Optional<T>` is now two partial specializations keyed on `TypeTraits<T>::storage_enabled`: - storage-enabled `T`: a 16-byte `TVMFFIAny`/`Any`-backed representation (`none == kTVMFFINone`), with explicit `Optional(const T&)` / `Optional(T&&)` constructors and `value()`/`value_or()`/`operator*` decoding via low-level `CopyFromAnyViewAfterCheck`/`MoveFromAnyAfterCheck`; - non-storage `T`: the existing `std::optional<T>` fallback (so use cases that must distinguish `nullopt` from a stored null keep `std::optional`). `TypeTraits<Optional<T>>` dispatches with `if constexpr (storage_enabled)`; the `Any` path reuses the `Any` without repacking. A short doc note documents the `Optional<T>` behavior: None/null reads back as `nullopt` (for a nullable `T`, null means no value), giving a stable cross-language ABI consistent with Python; use `std::optional` when `nullopt` must be distinguished from a stored null. **Rust**: mirror the C++ change with a single `#[repr(transparent)]` `Optional<T>` over the 16-byte `TVMFFIAny` cell in `option.rs` (named `Optional` to distinguish from `Option`), replacing the three per-layout mirrors. Observable changes: all `Any`-backed `Optional`/`Variant` are `sizeof(Any)` (16B); an all-`ObjectRef` `Variant` is no longer `ObjectRef`-derived. ## Testing Full C++ gtest suite passes (418/418); Rust `cargo test -p tvm-ffi` passes (incl. `test_optional`). clang-format/clang-tidy and cargo fmt/clippy clean on the changed files. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
