This is an automated email from the ASF dual-hosted git repository.
Seven-Streams pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/tvm-ffi.git
The following commit(s) were added to refs/heads/main by this push:
new 0c73a2e0 [STUBGEN] Rename the custom-new allocator to from_fields
(#870)
0c73a2e0 is described below
commit 0c73a2e01ee61ae96ed93e623e4562e0f9ad8468
Author: Linzhang Li <[email protected]>
AuthorDate: Thu Oct 22 15:46:21 2026 -0400
[STUBGEN] Rename the custom-new allocator to from_fields (#870)
## Summary
Rename the allocator that the Rust stub generator writes for a type
under the
`custom-new` directive from `from_complete_fields` to `from_fields`. The
directive leaves the wrapper's `new` to hand-written code, which usually
derives some fields and forwards to the generated allocator:
```rust
// tvm-ffi-stubgen(custom-new): rust_stubgen.IntPair
impl IntPair {
pub fn new(a: i64, b: i64) -> Self {
let kind = if a <= b { PairKind::Ordered } else {
PairKind::Unordered };
Self::from_fields(a, b, kind)
}
}
```
Types without the directive still get `new`, and `<Name>Obj::new` is
unchanged.
---
examples/rust_stubgen/README.md | 4 ++--
examples/rust_stubgen/rust/src/generated/rust_stubgen/mod.rs | 6 +++---
python/tvm_ffi/stub/rust_generator/codegen.py | 6 +++---
python/tvm_ffi/stub/rust_generator/directives.py | 2 +-
tests/python/test_stubgen_rust.py | 4 ++--
5 files changed, 11 insertions(+), 11 deletions(-)
diff --git a/examples/rust_stubgen/README.md b/examples/rust_stubgen/README.md
index 599de016..b089b79a 100644
--- a/examples/rust_stubgen/README.md
+++ b/examples/rust_stubgen/README.md
@@ -85,7 +85,7 @@ file the command line points at. This example keeps them under
## Hand-written constructors
`IntPair::new` is hand-written. The `custom-new` directive makes the generator
-name its lossless allocator `from_complete_fields` instead of `new`, and the
+name its lossless allocator `from_fields` instead of `new`, and the
hand-written constructor calls it after deriving `kind` from the operands:
```rust
@@ -94,7 +94,7 @@ hand-written constructor calls it after deriving `kind` from
the operands:
impl IntPair {
pub fn new(a: i64, b: i64) -> Self {
let kind = if a <= b { PairKind::Ordered } else { PairKind::Unordered
};
- Self::from_complete_fields(a, b, kind)
+ Self::from_fields(a, b, kind)
}
}
```
diff --git a/examples/rust_stubgen/rust/src/generated/rust_stubgen/mod.rs
b/examples/rust_stubgen/rust/src/generated/rust_stubgen/mod.rs
index cda7a4d6..de5110c2 100644
--- a/examples/rust_stubgen/rust/src/generated/rust_stubgen/mod.rs
+++ b/examples/rust_stubgen/rust/src/generated/rust_stubgen/mod.rs
@@ -35,7 +35,7 @@ use tvm_ffi::VALUE_ERROR;
// The `kind` field is an integer on the C++ side; this directive types it as
an
// open integer newtype in Rust.
// tvm-ffi-stubgen(enum): rust_stubgen.IntPair.kind -> PairKind(i32) {
Unordered=0, Ordered=1 }
-// `IntPair::new` is hand-written below, so the generated allocator is
`from_complete_fields`.
+// `IntPair::new` is hand-written below, so the generated allocator is
`from_fields`.
// tvm-ffi-stubgen(custom-new): rust_stubgen.IntPair
// tvm-ffi-stubgen(begin): object/rust_stubgen.IntPair
@@ -103,7 +103,7 @@ impl IntPairObj {
impl IntPair {
/// Lossless complete-field allocation.
- pub fn from_complete_fields(a: i64, b: i64, kind: PairKind) -> Self {
+ pub fn from_fields(a: i64, b: i64, kind: PairKind) -> Self {
let obj = IntPairObj::new(a, b, kind);
Self { base: ObjectArc::new(obj) }
}
@@ -116,6 +116,6 @@ impl IntPair {
/// `a <= b`, unordered otherwise.
pub fn new(a: i64, b: i64) -> Self {
let kind = if a <= b { PairKind::Ordered } else { PairKind::Unordered
};
- Self::from_complete_fields(a, b, kind)
+ Self::from_fields(a, b, kind)
}
}
diff --git a/python/tvm_ffi/stub/rust_generator/codegen.py
b/python/tvm_ffi/stub/rust_generator/codegen.py
index 6c735090..ebf568be 100644
--- a/python/tvm_ffi/stub/rust_generator/codegen.py
+++ b/python/tvm_ffi/stub/rust_generator/codegen.py
@@ -24,7 +24,7 @@ the verdict of :mod:`tvm_ffi.stub.layout`:
width, pinned by a ``const`` size/alignment assertion. ``<Leaf>Obj::new``
(crate-private) and the wrapper's ``new`` take every field root to leaf;
``custom-new`` leaves the wrapper's ``new`` to hand-written code and names
- the generated one ``from_complete_fields``.
+ the generated one ``from_fields``.
- *opaque*: the struct embeds only its parent, and each field is read through
the C ABI getter. Nothing allocates it.
@@ -498,7 +498,7 @@ class _ObjectRenderer:
def _allocator_sections(self, base: str, has_parent: bool) ->
list[list[str]]:
"""``<Leaf>Obj::new`` and the wrapper's ``new``.
- ``custom-new`` names the wrapper's allocator ``from_complete_fields``
instead.
+ ``custom-new`` names the wrapper's allocator ``from_fields`` instead.
"""
inherited: list[tuple[str, str]] = []
if has_parent:
@@ -530,7 +530,7 @@ class _ObjectRenderer:
f"impl {self.leaf} {{",
" /// Lossless complete-field allocation.",
*self._fn_lines(
- "pub fn from_complete_fields" if custom else "pub fn new",
+ "pub fn from_fields" if custom else "pub fn new",
params,
(f"obj = {self.obj_struct}::new", forward),
["base: ObjectArc::new(obj)"],
diff --git a/python/tvm_ffi/stub/rust_generator/directives.py
b/python/tvm_ffi/stub/rust_generator/directives.py
index 185b9065..ef2037ac 100644
--- a/python/tvm_ffi/stub/rust_generator/directives.py
+++ b/python/tvm_ffi/stub/rust_generator/directives.py
@@ -31,7 +31,7 @@ parameter instead. ``nullable`` wraps it in ``Option``;
``enum`` declares an ope
integer newtype for it; ``opaque`` keeps a type opaque even when its layout is
reproducible. ``upcast`` adds a typed view outside the ancestor chain;
``custom-new`` says the wrapper's ``new`` is hand-written; the generated one is
-named ``from_complete_fields`` instead.
+named ``from_fields`` instead.
"""
from __future__ import annotations
diff --git a/tests/python/test_stubgen_rust.py
b/tests/python/test_stubgen_rust.py
index 52b18c06..56d6a75f 100644
--- a/tests/python/test_stubgen_rust.py
+++ b/tests/python/test_stubgen_rust.py
@@ -1060,7 +1060,7 @@ def test_unrenderable_field_keeps_the_type_opaque(field:
NamedTypeSchema) -> Non
def test_custom_new_renames_the_wrapper_allocator() -> None:
- """`custom-new`: `Add::new` stays hand-written, the allocator is
`from_complete_fields`."""
+ """`custom-new`: `Add::new` stays hand-written, the allocator is
`from_fields`."""
_register(_expr())
imports = RUST.new_imports()
RUST.add_directive(imports, "custom-new", "tirx.Add", 1)
@@ -1071,7 +1071,7 @@ def test_custom_new_renames_the_wrapper_allocator() ->
None:
)
assert (
"impl Add {\n /// Lossless complete-field allocation.\n"
- " pub fn from_complete_fields(span: Span, ty: Type, a: Expr, b:
Expr) -> Self {\n"
+ " pub fn from_fields(span: Span, ty: Type, a: Expr, b: Expr) ->
Self {\n"
" let obj = AddObj::new(span, ty, a, b);\n"
" Self { base: ObjectArc::new(obj) }\n"
" }\n}"