tqchen commented on code in PR #684:
URL: https://github.com/apache/tvm-ffi/pull/684#discussion_r3642888220


##########
rust/tvm-ffi/src/object.rs:
##########
@@ -95,13 +99,120 @@ pub unsafe trait ObjectCoreWithExtraItems: ObjectCore {
 /// used by the ffi Any system and not user facing
 ///
 /// We mark as unsafe since it moves out the internal of the ObjectRef
+///
+/// # Safety
+///
+/// `data`, `into_data`, and `from_data` must preserve the same object
+/// allocation and form an ownership-preserving round trip. That allocation 
must
+/// start with a valid `TVMFFIObject` header whose registered object-range
+/// runtime type index correctly describes its layout and inheritance.
+///
+/// When `Self` also implements [`AnyCompatible`], `copy_to_any_view` must
+/// produce a non-owning view, while `move_to_any` must transfer ownership of
+/// the same object pointer and dynamic type index. `move_from_any_after_check`
+/// must be able to reclaim that owned representation exactly once, and a true
+/// `check_any_strict` result must guarantee that both after-check constructors
+/// are valid for it.
 pub unsafe trait ObjectRefCore: Sized + Clone {
     type ContainerType: ObjectCore;
     fn data(this: &Self) -> &ObjectArc<Self::ContainerType>;
     fn into_data(this: Self) -> ObjectArc<Self::ContainerType>;
     fn from_data(data: ObjectArc<Self::ContainerType>) -> Self;
 }
 
+/// Check whether a runtime type index refers to `target_type_index` or one of
+/// its subtypes.
+///
+/// The subtype relation lives in the process-wide type table maintained by the
+/// tvm-ffi library: every registered type records its depth in the single
+/// inheritance tree together with the chain of its ancestors. The check is
+/// O(1) — if `target` really is an ancestor, it must appear in the candidate's
+/// ancestor array exactly at `target`'s depth.
+///
+/// This is a hidden support function for derive-generated object checks. 
Object
+/// indices in the registered range must refer to entries in the runtime type
+/// table.
+#[doc(hidden)]
+#[inline(always)]
+pub fn is_instance_of(object_type_index: i32, target_type_index: i32) -> bool {
+    if object_type_index == target_type_index {
+        return true;
+    }
+    let object_begin = TypeIndex::kTVMFFIStaticObjectBegin as i32;
+    // Every registered object is an instance of the root Object type.
+    if target_type_index == object_begin {
+        return object_type_index >= object_begin;
+    }
+    // Only object types participate in the type hierarchy.
+    if object_type_index < object_begin || target_type_index < object_begin {
+        return false;
+    }
+    // Parent indices are always smaller than their descendants.
+    if object_type_index < target_type_index {
+        return false;
+    }
+    unsafe {
+        let object_info = TVMFFIGetTypeInfo(object_type_index);
+        let target_info = TVMFFIGetTypeInfo(target_type_index);
+        if object_info.is_null() || target_info.is_null() {
+            return false;
+        }
+        let target_depth = (*target_info).type_depth;

Review Comment:
   might worthcheck other template trick for target_type_index. Note in the 
case of C++ TargetType can be a type T, where things like type_depth can be 
directly queried as a compile time constant rather than runtime



##########
rust/tvm-ffi/src/object.rs:
##########
@@ -95,13 +99,120 @@ pub unsafe trait ObjectCoreWithExtraItems: ObjectCore {
 /// used by the ffi Any system and not user facing
 ///
 /// We mark as unsafe since it moves out the internal of the ObjectRef
+///
+/// # Safety
+///
+/// `data`, `into_data`, and `from_data` must preserve the same object
+/// allocation and form an ownership-preserving round trip. That allocation 
must
+/// start with a valid `TVMFFIObject` header whose registered object-range
+/// runtime type index correctly describes its layout and inheritance.
+///
+/// When `Self` also implements [`AnyCompatible`], `copy_to_any_view` must
+/// produce a non-owning view, while `move_to_any` must transfer ownership of
+/// the same object pointer and dynamic type index. `move_from_any_after_check`
+/// must be able to reclaim that owned representation exactly once, and a true
+/// `check_any_strict` result must guarantee that both after-check constructors
+/// are valid for it.
 pub unsafe trait ObjectRefCore: Sized + Clone {
     type ContainerType: ObjectCore;
     fn data(this: &Self) -> &ObjectArc<Self::ContainerType>;
     fn into_data(this: Self) -> ObjectArc<Self::ContainerType>;
     fn from_data(data: ObjectArc<Self::ContainerType>) -> Self;
 }
 
+/// Check whether a runtime type index refers to `target_type_index` or one of
+/// its subtypes.
+///
+/// The subtype relation lives in the process-wide type table maintained by the
+/// tvm-ffi library: every registered type records its depth in the single
+/// inheritance tree together with the chain of its ancestors. The check is
+/// O(1) — if `target` really is an ancestor, it must appear in the candidate's
+/// ancestor array exactly at `target`'s depth.
+///
+/// This is a hidden support function for derive-generated object checks. 
Object
+/// indices in the registered range must refer to entries in the runtime type
+/// table.
+#[doc(hidden)]
+#[inline(always)]
+pub fn is_instance_of(object_type_index: i32, target_type_index: i32) -> bool {
+    if object_type_index == target_type_index {
+        return true;
+    }
+    let object_begin = TypeIndex::kTVMFFIStaticObjectBegin as i32;
+    // Every registered object is an instance of the root Object type.
+    if target_type_index == object_begin {

Review Comment:
   given this is runtime check, likely not having the specialization is ok, 
depending on how freq conversion to ObjectRef is.



-- 
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]

Reply via email to