pub unsafe trait ObjectRefCore:
Sized
+ Clone
+ AnyCompatible {
type ContainerType: ObjectCore;
// Required methods
fn data(this: &Self) -> &ObjectArc<Self::ContainerType>;
fn into_data(this: Self) -> ObjectArc<Self::ContainerType>;
unsafe fn from_data(data: ObjectArc<Self::ContainerType>) -> Self;
// Provided methods
fn same_as<Other: ObjectRefCore>(&self, other: &Other) -> bool { ... }
fn type_key(&self) -> &'static str { ... }
fn is<T: AnyCompatible>(&self) -> bool { ... }
fn downcast_ref<T: DowncastRefFromObject>(&self) -> Option<&T> { ... }
fn downcast<B>(self) -> Result<B, Self>
where B: ObjectRefCore { ... }
}Expand description
A managed object reference, such as ObjectRef, Array<T>
or a #[derive(ObjectRef)] type, with identity (same_as)
and strict casts that check the runtime type and never convert; upcasts are
From/Into.
§Casts
downcast_refborrows, strictly, as std’sdyn Any::downcast_refdoes.downcasttakes the value, strictly, and hands it back on a mismatch, asBox<dyn Any>::downcastdoes.TryFromtakes the value and returns aTypeError, for?. Between references it only checks; from anAnyit converts by the FFI rules, such as an integer into a float.
§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.
Required Associated Types§
Sourcetype ContainerType: ObjectCore
type ContainerType: ObjectCore
The object type the reference points to.
Required Methods§
Sourcefn data(this: &Self) -> &ObjectArc<Self::ContainerType>
fn data(this: &Self) -> &ObjectArc<Self::ContainerType>
Borrow the object’s handle.
Sourcefn into_data(this: Self) -> ObjectArc<Self::ContainerType>
fn into_data(this: Self) -> ObjectArc<Self::ContainerType>
Take the object’s handle.
Sourceunsafe fn from_data(data: ObjectArc<Self::ContainerType>) -> Self
unsafe fn from_data(data: ObjectArc<Self::ContainerType>) -> Self
Make a reference from the object’s handle.
§Safety
In addition to containing a valid ContainerType allocation, data
must satisfy every semantic invariant imposed by Self. This matters
for zero-state views that share a container type but accept only a
subset of its values, such as a reference whose
ObjectRefAnyCompatible::extra_check rejects some objects.
Provided Methods§
Sourcefn same_as<Other: ObjectRefCore>(&self, other: &Other) -> bool
fn same_as<Other: ObjectRefCore>(&self, other: &Other) -> bool
Whether two references point to the same object.
Sourcefn type_key(&self) -> &'static str
fn type_key(&self) -> &'static str
The registered type key of the object’s runtime type, such as
"ffi.Array", as C++ ObjectRef::GetTypeKey.
Sourcefn is<T: AnyCompatible>(&self) -> bool
fn is<T: AnyCompatible>(&self) -> bool
Whether the reference is a T as is, such as a subtype or a container
whose elements are checked too, by the strict check of
downcast, without a reference count change.
See also crate::Any::is.
Sourcefn downcast_ref<T: DowncastRefFromObject>(&self) -> Option<&T>
fn downcast_ref<T: DowncastRefFromObject>(&self) -> Option<&T>
Borrow the underlying object as T when it is one.
T is an object type such as MyPairObj, or a managed reference such
as MyPair: &ObjectRef borrows as &MyPair when its object is a
MyPairObj. Unrelated types return None. Unlike downcast, this
method neither consumes the reference nor changes the object’s reference
count. The returned borrow cannot outlive self.
See crate::Any::downcast_ref for an example.
Sourcefn downcast<B>(self) -> Result<B, Self>where
B: ObjectRefCore,
fn downcast<B>(self) -> Result<B, Self>where
B: ObjectRefCore,
Take self as B, a subtype or a container such as Array<i64> whose
elements are checked too, when its object is one, without copying it.
On a mismatch the reference is returned unchanged.
use tvm_ffi::{object::ObjectRef, Array, ObjectRefCore};
let object: ObjectRef = Array::from_vec(vec![1_i64]).into();
let object = object.downcast::<Array<f64>>().unwrap_err();
let array: Array<i64> = object.try_into()?;
assert_eq!(array.len(), 1);Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".