Skip to main content

ObjectRefCore

Trait ObjectRefCore 

Source
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_ref borrows, strictly, as std’s dyn Any::downcast_ref does.
  • downcast takes the value, strictly, and hands it back on a mismatch, as Box<dyn Any>::downcast does.
  • TryFrom takes the value and returns a TypeError, for ?. Between references it only checks; from an Any it 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§

Source

type ContainerType: ObjectCore

The object type the reference points to.

Required Methods§

Source

fn data(this: &Self) -> &ObjectArc<Self::ContainerType>

Borrow the object’s handle.

Source

fn into_data(this: Self) -> ObjectArc<Self::ContainerType>

Take the object’s handle.

Source

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§

Source

fn same_as<Other: ObjectRefCore>(&self, other: &Other) -> bool

Whether two references point to the same object.

Source

fn type_key(&self) -> &'static str

The registered type key of the object’s runtime type, such as "ffi.Array", as C++ ObjectRef::GetTypeKey.

Source

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.

Source

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.

Source

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".

Implementors§