Borrow the stored object as T when its runtime type is T or a subtype.
T is an object representation type such as FooObj, rather than its
managed reference Foo. Non-object values and unrelated object types
return None. This neither consumes self nor changes the object’s
reference count, and the returned reference cannot outlive self.
use tvm_ffi::{def_object_ref, rs_class, Any, AnyView, Object, ObjectRefCore};
use tvm_ffi::object::ObjectRef;
#[rs_class("my.MyPair", crate = tvm_ffi)]
pub struct MyPairObj {
base: Object,
pub first: i64,
pub second: i64,
}
def_object_ref!(pub MyPair, ObjectRef, MyPairObj, crate = tvm_ffi);
let pair = MyPair::from(MyPairObj::new(Object::new(), 3, 4));
assert_eq!(pair.downcast_ref::<MyPairObj>().unwrap().first, 3);
let value = Any::from(pair);
assert_eq!(value.downcast_ref::<MyPairObj>().unwrap().second, 4);
let view = AnyView::from(&value);
assert_eq!(view.downcast_ref::<MyPairObj>().unwrap().first, 3);
Try to query if stored typed in Any exactly matches the type T
This function is fast in the case of failure and can be used to check
if the type is compatible with T
This function will not try to cast the type
and ensures invariance that the return value is only Some(T)
Any::from(T) contains exactly the same value value
Any::try_as<T>() contains exactly the same value value
will return Some(T) if the type is exactly compatible with T
will return None if the type is not compatible with T