pub struct AnyView<'a> { /* private fields */ }Expand description
A borrowed, type-erased FFI value.
AnyView holds any FFI value, such as an integer, a string or an object,
without owning it: it does not retain objects, and its lifetime 'a keeps
it from outliving the value it borrows. It is Copy and is the argument
type of FFI calls. Convert it into an Any to own the value.
§Examples
use tvm_ffi::{Any, AnyView, Array};
let array = Array::from_vec(vec![1_i64, 2, 3]);
let view = AnyView::from(&array);
assert_eq!(view.downcast_ref::<Array<i64>>().unwrap().len(), 3);
let owned = Any::from(view);
assert_eq!(Array::<i64>::try_from(owned)?.len(), 3);Implementations§
Source§impl<'a> AnyView<'a>
impl<'a> AnyView<'a>
Sourcepub fn type_index(&self) -> i32
pub fn type_index(&self) -> i32
Return the runtime type index of the viewed 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 viewed value, such as "int" or
"ffi.Array". See also Any::type_key.
Sourcepub fn as_raw_ffi_any(&self) -> &TVMFFIAny
pub fn as_raw_ffi_any(&self) -> &TVMFFIAny
Borrow the raw ABI representation. Copying it does not retain referenced data or extend its lifetime.
Sourcepub unsafe fn from_raw_ffi_any(data: TVMFFIAny) -> Self
pub unsafe fn from_raw_ffi_any(data: TVMFFIAny) -> Self
Construct a borrowed view from its ABI representation.
§Safety
data.type_index must describe the payload, and the caller must keep
every resource it references alive for the view’s complete lifetime.
kTVMFFIObjectRValueRef carries a further obligation: v_ptr must
point to a writable slot the caller uniquely owns, holding one strong
reference. Owning the view takes that reference and writes null back
through the pointer, so the view must be converted at most once and no
other access to the slot may overlap the conversion.
Sourcepub fn downcast_ref<T: DowncastRefFromAny>(&self) -> Option<&T>
pub fn downcast_ref<T: DowncastRefFromAny>(&self) -> Option<&T>
Borrow the viewed 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.
The borrow does not change any reference count and is tied to this view,
which holds its own copy of the value’s cell.
§Returns
The borrowed value, or None.
See also Any::downcast_ref.
Sourcepub fn is<T: AnyCompatible>(&self) -> bool
pub fn is<T: AnyCompatible>(&self) -> bool
Whether the viewed value is a T as is, by the strict check of
downcast_ref, without conversion.
See also Any::is.
Trait Implementations§
Source§impl Display for AnyView<'_>
Formats the value as its repr, the text the runtime printer gives in
every language, such as (1, 2) for an array or "hi" for a string.
Object references format the same way.
impl Display for AnyView<'_>
Formats the value as its repr, the text the runtime printer gives in
every language, such as (1, 2) for an array or "hi" for a string.
Object references format the same way.
§Examples
use tvm_ffi::{AnyView, Array};
let xs = Array::from_vec(vec![1_i64, 2]);
assert_eq!(AnyView::from(&xs).to_string(), "(1, 2)");
assert_eq!(xs.to_string(), "(1, 2)");Source§impl<'a> From<&'a AccessPathObj> for AnyView<'a>
impl<'a> From<&'a AccessPathObj> for AnyView<'a>
Source§fn from(value: &'a AccessPathObj) -> Self
fn from(value: &'a AccessPathObj) -> Self
Source§impl<'a> From<&'a AccessStepObj> for AnyView<'a>
impl<'a> From<&'a AccessStepObj> for AnyView<'a>
Source§fn from(value: &'a AccessStepObj) -> Self
fn from(value: &'a AccessStepObj) -> Self
Source§impl<'a> From<&'a FunctionObj> for AnyView<'a>
impl<'a> From<&'a FunctionObj> for AnyView<'a>
Source§fn from(value: &'a FunctionObj) -> Self
fn from(value: &'a FunctionObj) -> Self
Source§impl<'a, T> From<&'a MutationView<'_, T>> for AnyView<'a>
impl<'a, T> From<&'a MutationView<'_, T>> for AnyView<'a>
Source§fn from(value: &'a MutationView<'_, T>) -> Self
fn from(value: &'a MutationView<'_, T>) -> Self
Source§impl<'a> From<&TensorView<'a>> for AnyView<'a>
impl<'a> From<&TensorView<'a>> for AnyView<'a>
Source§fn from(view: &TensorView<'a>) -> Self
fn from(view: &TensorView<'a>) -> Self
Source§impl<'a> From<&'a VisitInterruptObj> for AnyView<'a>
impl<'a> From<&'a VisitInterruptObj> for AnyView<'a>
Source§fn from(value: &'a VisitInterruptObj) -> Self
fn from(value: &'a VisitInterruptObj) -> Self
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.