macro_rules! impl_object_ref_downcast_ref {
(unsafe $($name:ty),+ $(,)?) => { ... };
}Expand description
Implement the in-place downcast_ref borrows (array[i], Any::downcast_ref,
ObjectRefCore::downcast_ref) for each
listed object reference type. #[derive(ObjectRef)]
emits it; a reference that implements ObjectRefCore by hand calls it.
§Safety
The leading unsafe is a required marker: writing it, the caller promises
that each type is repr(transparent) over one object pointer, and its
AnyCompatible::check_any_strict
accepts exactly the objects that are a valid value of it.
§Examples
use tvm_ffi::derive::ObjectRef;
use tvm_ffi::{rs_object, impl_object_ref_downcast_ref, Any};
use tvm_ffi::{Object, ObjectArc, ObjectRefCore};
use tvm_ffi::object::ObjectRefAnyCompatible;
#[rs_object(type_key = "my_ext.MyPair", final)]
pub struct MyPairObj {
base_: Object,
pub first: i64,
pub second: i64,
}
#[derive(Clone, ObjectRef)]
pub struct MyPair {
data: ObjectArc<MyPairObj>,
}
/// A `MyPair` whose `first` is positive.
#[derive(Clone)]
#[repr(transparent)]
pub struct PositivePair(ObjectArc<MyPairObj>);
unsafe impl ObjectRefCore for PositivePair {
type ContainerType = MyPairObj;
fn data(this: &Self) -> &ObjectArc<MyPairObj> {
&this.0
}
fn into_data(this: Self) -> ObjectArc<MyPairObj> {
this.0
}
unsafe fn from_data(data: ObjectArc<MyPairObj>) -> Self {
Self(data)
}
}
unsafe impl ObjectRefAnyCompatible for PositivePair {
fn extra_check(pair: &MyPairObj) -> bool {
pair.first > 0
}
}
impl_object_ref_downcast_ref!(unsafe PositivePair);
let pair = Any::from(MyPair::from_object(MyPairObj::new(Object::new(), 1, 2)));
assert!(pair.downcast_ref::<PositivePair>().is_some());