Skip to main content

AnyView

Struct AnyView 

Source
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>

Source

pub fn new() -> Self

Create a view of None, the empty FFI value.

§Returns

A view holding None.

Source

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.

Source

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.

Source

pub fn is_none(&self) -> bool

Whether the value is None.

Source

pub fn as_raw_ffi_any(&self) -> &TVMFFIAny

Borrow the raw ABI representation. Copying it does not retain referenced data or extend its lifetime.

Source

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.

Source

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.

Source

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<'a> Clone for AnyView<'a>

Source§

fn clone(&self) -> AnyView<'a>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for AnyView<'_>

Formats the type key and the repr, such as AnyView(int: 1).

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for AnyView<'_>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
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.

§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§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'a> From<&'a AccessPathObj> for AnyView<'a>

Source§

fn from(value: &'a AccessPathObj) -> Self

Converts to this type from the input type.
Source§

impl<'a> From<&'a AccessStepObj> for AnyView<'a>

Source§

fn from(value: &'a AccessStepObj) -> Self

Converts to this type from the input type.
Source§

impl<'a> From<&'a Any> for AnyView<'a>

Source§

fn from(value: &'a Any) -> Self

Converts to this type from the input type.
Source§

impl<'a> From<&AnyView<'a>> for AnyView<'a>

Source§

fn from(value: &AnyView<'a>) -> Self

Converts to this type from the input type.
Source§

impl<'a> From<&'a ArrayObj> for AnyView<'a>

Source§

fn from(value: &'a ArrayObj) -> Self

Converts to this type from the input type.
Source§

impl<'a> From<&'a EnumObj> for AnyView<'a>

Source§

fn from(value: &'a EnumObj) -> Self

Converts to this type from the input type.
Source§

impl<'a> From<&'a ErrorObj> for AnyView<'a>

Source§

fn from(value: &'a ErrorObj) -> Self

Converts to this type from the input type.
Source§

impl<'a> From<&'a FunctionObj> for AnyView<'a>

Source§

fn from(value: &'a FunctionObj) -> Self

Converts to this type from the input type.
Source§

impl<'a> From<&'a ListObj> for AnyView<'a>

Source§

fn from(value: &'a ListObj) -> Self

Converts to this type from the input type.
Source§

impl<'a> From<&'a MapObj> for AnyView<'a>

Source§

fn from(value: &'a MapObj) -> Self

Converts to this type from the input type.
Source§

impl<'a> From<&'a ModuleObj> for AnyView<'a>

Source§

fn from(value: &'a ModuleObj) -> Self

Converts to this type from the input type.
Source§

impl<'a, T> From<&'a MutationView<'_, T>> for AnyView<'a>

Source§

fn from(value: &'a MutationView<'_, T>) -> Self

Converts to this type from the input type.
Source§

impl<'a> From<&'a Object> for AnyView<'a>

Source§

fn from(value: &'a Object) -> Self

Converts to this type from the input type.
Source§

impl<'a, T> From<&'a RValueRef<T>> for AnyView<'a>
where T: ObjectRefCore,

Source§

fn from(value: &'a RValueRef<T>) -> Self

Converts to this type from the input type.
Source§

impl<'a> From<&'a ShapeObj> for AnyView<'a>

Source§

fn from(value: &'a ShapeObj) -> Self

Converts to this type from the input type.
Source§

impl<'a, T: AnyCompatible> From<&'a T> for AnyView<'a>

Source§

fn from(value: &'a T) -> Self

Converts to this type from the input type.
Source§

impl<'a> From<&'a TensorObj> for AnyView<'a>

Source§

fn from(value: &'a TensorObj) -> Self

Converts to this type from the input type.
Source§

impl<'a> From<&TensorView<'a>> for AnyView<'a>

Source§

fn from(view: &TensorView<'a>) -> Self

Converts to this type from the input type.
Source§

impl<'a> From<&'a VisitInterruptObj> for AnyView<'a>

Source§

fn from(value: &'a VisitInterruptObj) -> Self

Converts to this type from the input type.
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.

A DLTensor* has no reference count to take, so it is kept as is, as in C++: the Any borrows the tensor and must not outlive it. Convert it to a Tensor to own the tensor.

§Panics

Panics if the view holds an RValueRef whose object an earlier conversion already took.

Source§

fn from(value: AnyView<'_>) -> Self

Converts to this type from the input type.
Source§

impl<T> TryFrom<AnyView<'_>> for RValueRef<T>
where T: ObjectRefCore,

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(value: AnyView<'_>) -> Result<Self>

Performs the conversion.
Source§

impl<'a> TryFrom<AnyView<'a>> for ()

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(value: AnyView<'a>) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl<'a> TryFrom<AnyView<'a>> for *mut c_void

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(value: AnyView<'a>) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl<'a> TryFrom<AnyView<'a>> for AccessPath

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(value: AnyView<'a>) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl<'a> TryFrom<AnyView<'a>> for AccessStep

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(value: AnyView<'a>) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl<'a, T> TryFrom<AnyView<'a>> for Array<T>

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(value: AnyView<'a>) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl<'a> TryFrom<AnyView<'a>> for BigInt

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(value: AnyView<'a>) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl<'a> TryFrom<AnyView<'a>> for Bytes

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(value: AnyView<'a>) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl<'a> TryFrom<AnyView<'a>> for DLDataType

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(value: AnyView<'a>) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl<'a> TryFrom<AnyView<'a>> for DLDevice

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(value: AnyView<'a>) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl<'a> TryFrom<AnyView<'a>> for Error

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(value: AnyView<'a>) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl<'a> TryFrom<AnyView<'a>> for Function

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(value: AnyView<'a>) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl<'a, T: ContainerElement> TryFrom<AnyView<'a>> for List<T>

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(value: AnyView<'a>) -> Result<Self>

Performs the conversion.
Source§

impl<'a, K, V> TryFrom<AnyView<'a>> for Map<K, V>

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(value: AnyView<'a>) -> Result<Self>

Performs the conversion.
Source§

impl<'a> TryFrom<AnyView<'a>> for Module

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(value: AnyView<'a>) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl<'a> TryFrom<AnyView<'a>> for ObjectRef

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(value: AnyView<'a>) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl<'a, T: AnyCompatible> TryFrom<AnyView<'a>> for Option<T>

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(value: AnyView<'a>) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl<'a> TryFrom<AnyView<'a>> for Shape

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(value: AnyView<'a>) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl<'a> TryFrom<AnyView<'a>> for String

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(value: AnyView<'a>) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl<'a> TryFrom<AnyView<'a>> for Tensor

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(value: AnyView<'a>) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl<'a> TryFrom<AnyView<'a>> for TensorView<'a>

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(value: AnyView<'a>) -> Result<Self>

Performs the conversion.
Source§

impl<'a> TryFrom<AnyView<'a>> for Unchanged

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(value: AnyView<'a>) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl<'a, T: ContainerElement> TryFrom<AnyView<'a>> for UnchangedOr<T>

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(value: AnyView<'a>) -> Result<Self>

Performs the conversion.
Source§

impl<'a> TryFrom<AnyView<'a>> for VisitInterrupt

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(value: AnyView<'a>) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl<'a> TryFrom<AnyView<'a>> for bool

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(value: AnyView<'a>) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl<'a> TryFrom<AnyView<'a>> for f32

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(value: AnyView<'a>) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl<'a> TryFrom<AnyView<'a>> for f64

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(value: AnyView<'a>) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl<'a> TryFrom<AnyView<'a>> for i16

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(value: AnyView<'a>) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl<'a> TryFrom<AnyView<'a>> for i32

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(value: AnyView<'a>) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl<'a> TryFrom<AnyView<'a>> for i64

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(value: AnyView<'a>) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl<'a> TryFrom<AnyView<'a>> for i8

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(value: AnyView<'a>) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl<'a> TryFrom<AnyView<'a>> for isize

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(value: AnyView<'a>) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl<'a> TryFrom<AnyView<'a>> for u16

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(value: AnyView<'a>) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl<'a> TryFrom<AnyView<'a>> for u32

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(value: AnyView<'a>) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl<'a> TryFrom<AnyView<'a>> for u64

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(value: AnyView<'a>) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl<'a> TryFrom<AnyView<'a>> for u8

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(value: AnyView<'a>) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl<'a> TryFrom<AnyView<'a>> for usize

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(value: AnyView<'a>) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl<'a> Copy for AnyView<'a>

Auto Trait Implementations§

§

impl<'a> Freeze for AnyView<'a>

§

impl<'a> RefUnwindSafe for AnyView<'a>

§

impl<'a> !Send for AnyView<'a>

§

impl<'a> !Sync for AnyView<'a>

§

impl<'a> Unpin for AnyView<'a>

§

impl<'a> UnsafeUnpin for AnyView<'a>

§

impl<'a> UnwindSafe for AnyView<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> TypeSubsumes<T> for T
where T: ?Sized,