pub struct Any { /* private fields */ }Expand description
An owned, type-erased FFI value.
Any holds any FFI value, such as an integer, a string or an object, and
owns it: cloning an Any that holds an object retains the object, and
dropping it releases the object. Any AnyCompatible value converts into
it with From; TryFrom (converting), downcast_ref
(borrowing) and match_any! read it back. Borrow it as an AnyView to pass it without retaining.
The one value it does not own is a DLTensor* converted from an
AnyView, as in C++: that Any borrows the tensor and must not outlive
it.
§Examples
use tvm_ffi::Any;
let value = Any::from(42_i64);
assert_eq!(i64::try_from(&value)?, 42);
assert!(tvm_ffi::String::try_from(&value).is_err());
let text = Any::from(tvm_ffi::String::from("hello"));
assert_eq!(text.downcast_ref::<tvm_ffi::String>().unwrap().as_str(), "hello");Implementations§
Source§impl Any
impl Any
Sourcepub fn type_index(&self) -> i32
pub fn type_index(&self) -> i32
Return the runtime type index of the stored value.
Indices below TypeIndex::kTVMFFIStaticObjectBegin are the built-in
non-object types; the others are object types.
§Returns
The type index.
Sourcepub fn type_key(&self) -> &'static str
pub fn type_key(&self) -> &'static str
The registered type key of the stored value, such as "int" or
"ffi.Array", as C++ Any::GetTypeKey.
use tvm_ffi::{Any, Array};
assert_eq!(Any::from(1_i64).type_key(), "int");
assert_eq!(Any::from(Array::from_vec(vec![1_i64])).type_key(), "ffi.Array");Sourcepub fn as_raw_ffi_any(&self) -> &TVMFFIAny
pub fn as_raw_ffi_any(&self) -> &TVMFFIAny
Borrow the raw ABI representation, which this value owns. Copying it does not retain referenced data or extend its lifetime.
Sourcepub fn downcast_ref<T: DowncastRefFromAny>(&self) -> Option<&T>
pub fn downcast_ref<T: DowncastRefFromAny>(&self) -> Option<&T>
Borrow the stored value as T when it holds one, without copying it.
T is an object type such as ArrayObj, a managed reference such as
Array<i64>, or a value stored in place such as String or i64. The
check is strict and never converts, unlike TryFrom.
This neither consumes self nor changes any reference count, and the
returned reference cannot outlive self.
§Returns
The borrowed object, or None.
use tvm_ffi::{collections::array::ArrayObj, Any, Array};
let value = Any::from(Array::from_vec(vec![1_i64, 2]));
assert_eq!(value.downcast_ref::<ArrayObj>().unwrap().size, 2);
assert_eq!(value.downcast_ref::<Array<i64>>().unwrap()[1], 2);
assert!(value.downcast_ref::<i64>().is_none());See also match_any!.
Sourcepub fn is<T: AnyCompatible>(&self) -> bool
pub fn is<T: AnyCompatible>(&self) -> bool
Whether the stored value is a T as is, by the strict check of
downcast_ref, without conversion, as std
Any::is. It covers every AnyCompatible type, such as bool.
use tvm_ffi::{Any, Array};
let value = Any::from(Array::from_vec(vec![1_i64, 2]));
assert!(value.is::<Array<i64>>());
assert!(!value.is::<Array<f64>>());
assert!(Any::from(true).is::<bool>());
assert!(!Any::from(1_i64).is::<f64>());Sourcepub fn as_raw_ffi_any_mut(&mut self) -> *mut TVMFFIAny
pub fn as_raw_ffi_any_mut(&mut self) -> *mut TVMFFIAny
Return a pointer to the raw ABI representation, for a runtime call that writes an owned result into it as an out-parameter.
Writing through the pointer is unsafe: the cell must afterwards hold a
value this Any may own and release, and the previous value must have
been released or moved out.
Sourcepub fn into_raw_ffi_any(self) -> TVMFFIAny
pub fn into_raw_ffi_any(self) -> TVMFFIAny
Give up ownership of the value as its raw ABI representation, as
Box::into_raw does. Rebuild it with from_raw_ffi_any
to release it.
Sourcepub unsafe fn from_raw_ffi_any(data: TVMFFIAny) -> Self
pub unsafe fn from_raw_ffi_any(data: TVMFFIAny) -> Self
Take ownership of a raw ABI representation.
§Safety
data.type_index must describe the payload, and the caller must hand
over the ownership data carries, such as one strong reference for an
object, so it is released exactly once, by this Any.
Trait Implementations§
Source§impl DowncastRefFromAny for Any
impl DowncastRefFromAny for Any
Source§unsafe fn downcast_ref_from_any(cell: &TVMFFIAny) -> Option<&Self>
unsafe fn downcast_ref_from_any(cell: &TVMFFIAny) -> Option<&Self>
Source§impl<'a> From<&'a mut Any> for MutationView<'a>
impl<'a> From<&'a mut Any> for MutationView<'a>
Source§impl From<AnyView<'_>> for Any
Takes ownership of the viewed value: objects gain a reference, borrowed
strings and bytes are copied, and an RValueRef
gives up its object.
impl From<AnyView<'_>> for Any
Takes ownership of the viewed value: objects gain a reference, borrowed
strings and bytes are copied, and an RValueRef
gives up its object.