pub struct Array<T> { /* private fields */ }Expand description
A reference-counted sequence of T with value semantics, passed across
languages as ffi.Array.
Cloning an Array is cheap: it shares the same elements. The mutators
(push, insert, pop, truncate, set) are copy-on-write:
they copy the elements when the array is shared, and write in place when
this handle is the only reference, so other handles never see the change.
Use List for a sequence whose changes all handles see.
§Examples
use tvm_ffi::Array;
let mut array = Array::from_vec(vec![1_i64, 2, 3]);
assert_eq!(array.len(), 3);
assert_eq!(array.get(1), Some(2));
assert_eq!(array.get(3), None);
let snapshot = array.clone();
array.push(4);
assert_eq!(array.len(), 4);
assert_eq!(snapshot.len(), 3);
let doubled: Array<i64> = array.iter().map(|x| x * 2).collect();
assert_eq!(doubled.iter().copied().collect::<Vec<_>>(), vec![2, 4, 6, 8]);Implementations§
Source§impl<T> Array<T>
impl<T> Array<T>
Sourcepub fn upcast<U: TypeSubsumes<T>>(self) -> Array<U>
pub fn upcast<U: TypeSubsumes<T>>(self) -> Array<U>
View this array as an Array<U> without a copy, as C++
Array<U>(const Array<T>&) does for a base U of T.
It is a method, not From: From<Array<T>> for Array<U> would overlap
From<T> for T at T = U (E0119), and a crate that defines T and U
cannot implement it for Array either (orphan rule, E0117).
use tvm_ffi::object::{ObjectRef, ObjectRefCore};
use tvm_ffi::{Array, Shape};
let shapes = Array::from([Shape::from([1_i64, 2])]);
let objects: Array<ObjectRef> = shapes.clone().upcast();
assert!(objects[0].same_as(&shapes[0]));Sourcepub fn upcast_ref<U: TypeSubsumes<T>>(&self) -> &Array<U>
pub fn upcast_ref<U: TypeSubsumes<T>>(&self) -> &Array<U>
Borrow this array as an Array<U>, as upcast does.
Source§impl<T: ContainerElement + Clone> Array<T>
impl<T: ContainerElement + Clone> Array<T>
Sourcepub fn push(&mut self, value: T)
pub fn push(&mut self, value: T)
Append value to the end.
See also List::push.
Sourcepub fn insert(&mut self, index: usize, value: T)
pub fn insert(&mut self, index: usize, value: T)
Insert value at index, shifting the elements after it, as
Vec::insert.
§Panics
Panics if index > len.
See also List::insert.
Sourcepub fn truncate(&mut self, len: usize)
pub fn truncate(&mut self, len: usize)
Shorten the array to len elements, dropping the rest; a no-op when it
is already that short.
See also List::truncate.
Sourcepub fn get(&self, index: usize) -> Option<T>
pub fn get(&self, index: usize) -> Option<T>
Return the element at index, or None if it is out of bounds, as
slice::get. array[index] borrows it instead, and panics when out
of bounds.
Source§impl<T: ContainerElement + Clone + RefFromAnyUnchecked> Array<T>
impl<T: ContainerElement + Clone + RefFromAnyUnchecked> Array<T>
Sourcepub fn iter(&self) -> Iter<'_, T> ⓘ
pub fn iter(&self) -> Iter<'_, T> ⓘ
Return an iterator that borrows each element in place, without a
reference-count bump, as array[i] does.
Element types stored in another representation, such as bool or
i32 (held as i64), iterate by value instead: for x in array.
use tvm_ffi::Array;
let array = Array::from_vec(vec![1_i64, 2, 3]);
assert_eq!(array.iter().sum::<i64>(), 6);Trait Implementations§
Source§impl<T> AnyCompatible for Array<T>where
T: ContainerElement + Clone,
impl<T> AnyCompatible for Array<T>where
T: ContainerElement + Clone,
Source§unsafe fn is_undefined(field: *const Self) -> bool
unsafe fn is_undefined(field: *const Self) -> bool
field, which holds a value of this type or all-zero bytes,
holds all-zero bytes that are no value of this type, as a null object
reference, a string of type index None or a zero device type: a field
that a creator left undefined for reflection to set. An undefined field
is no value; a nullable reference is Option<T>, whose None is a
defined value. Read moreSource§fn type_str() -> String
fn type_str() -> String
int,
ffi.String or Array<int>.Source§fn type_schema() -> String
fn type_schema() -> String
Source§unsafe fn check_any_strict(data: &TVMFFIAny) -> bool
unsafe fn check_any_strict(data: &TVMFFIAny) -> bool
Source§unsafe fn copy_to_any_view(src: &Self, data: &mut TVMFFIAny)
unsafe fn copy_to_any_view(src: &Self, data: &mut TVMFFIAny)
Source§unsafe fn move_to_any(src: Self, data: &mut TVMFFIAny)
unsafe fn move_to_any(src: Self, data: &mut TVMFFIAny)
Source§unsafe fn copy_from_any_view_after_check(data: &TVMFFIAny) -> Self
unsafe fn copy_from_any_view_after_check(data: &TVMFFIAny) -> Self
Self out of a cell that holds one, retaining any object. Read moreSource§unsafe fn move_from_any_after_check(data: &mut TVMFFIAny) -> Self
unsafe fn move_from_any_after_check(data: &mut TVMFFIAny) -> Self
Self out of an owning cell that holds one. Read moreSource§unsafe fn try_cast_from_any_view(data: &TVMFFIAny) -> Result<Self, ()>
unsafe fn try_cast_from_any_view(data: &TVMFFIAny) -> Result<Self, ()>
data into a Self by the FFI rules, such as an integer into a
float; Err if it does not convert. Read moreSource§fn get_mismatch_type_info(data: &TVMFFIAny) -> String
fn get_mismatch_type_info(data: &TVMFFIAny) -> String
data in conversion errors; by
default its runtime type key.Source§impl<T: ContainerElement + Clone> Display for Array<T>
Formats the array as its repr; see AnyView.
impl<T: ContainerElement + Clone> Display for Array<T>
Formats the array as its repr; see AnyView.
Source§impl<T> DowncastRefFromAny for Array<T>where
T: ContainerElement + Clone,
impl<T> DowncastRefFromAny for Array<T>where
T: ContainerElement + Clone,
Source§impl<T: ContainerElement + Clone> From<&Array<T>> for Array<T>
A reference-count bump, as From<&T> for T of an object reference.
impl<T: ContainerElement + Clone> From<&Array<T>> for Array<T>
A reference-count bump, as From<&T> for T of an object reference.
Source§impl<T: ContainerElement + Clone> From<Array<T>> for ObjectRef
An array upcasts to the root ObjectRef.
impl<T: ContainerElement + Clone> From<Array<T>> for ObjectRef
An array upcasts to the root ObjectRef.
Source§impl<T: ContainerElement + Clone> FromIterator<T> for Array<T>
impl<T: ContainerElement + Clone> FromIterator<T> for Array<T>
Source§fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self
Source§impl<T: ContainerElement + Clone + RefFromAnyUnchecked> Index<usize> for Array<T>
Lends &T from the array’s cell in place. Every cell of an Array<T>
holds a T (checked when the array is built), so only a debug check runs.
impl<T: ContainerElement + Clone + RefFromAnyUnchecked> Index<usize> for Array<T>
Lends &T from the array’s cell in place. Every cell of an Array<T>
holds a T (checked when the array is built), so only a debug check runs.
Source§impl<'a, T, U: TypeSubsumes<T> + ContainerElement + Clone + 'a> IntoArg<Array<U>> for &'a Array<T>
impl<'a, T, U: TypeSubsumes<T> + ContainerElement + Clone + 'a> IntoArg<Array<U>> for &'a Array<T>
Source§impl<'a, T: ContainerElement + Clone + RefFromAnyUnchecked> IntoIterator for &'a Array<T>
impl<'a, T: ContainerElement + Clone + RefFromAnyUnchecked> IntoIterator for &'a Array<T>
Source§impl<T: ContainerElement + Clone> IntoIterator for Array<T>
Iterate by value, for every element type. Cloning an Array first costs one
reference-count bump for the whole array.
impl<T: ContainerElement + Clone> IntoIterator for Array<T>
Iterate by value, for every element type. Cloning an Array first costs one
reference-count bump for the whole array.
Source§impl<T: ContainerElement + Clone> ObjectRefCore for Array<T>
impl<T: ContainerElement + Clone> ObjectRefCore for Array<T>
Source§type ContainerType = ArrayObj
type ContainerType = ArrayObj
Source§fn data(this: &Self) -> &ObjectArc<Self::ContainerType>
fn data(this: &Self) -> &ObjectArc<Self::ContainerType>
Source§fn into_data(this: Self) -> ObjectArc<Self::ContainerType>
fn into_data(this: Self) -> ObjectArc<Self::ContainerType>
Source§unsafe fn from_data(data: ObjectArc<Self::ContainerType>) -> Self
unsafe fn from_data(data: ObjectArc<Self::ContainerType>) -> Self
Source§fn same_as<Other: ObjectRefCore>(&self, other: &Other) -> bool
fn same_as<Other: ObjectRefCore>(&self, other: &Other) -> bool
Source§fn type_key(&self) -> &'static str
fn type_key(&self) -> &'static str
"ffi.Array", as C++ ObjectRef::GetTypeKey.Source§fn is<T: AnyCompatible>(&self) -> bool
fn is<T: AnyCompatible>(&self) -> bool
T as is, such as a subtype or a container
whose elements are checked too, by the strict check of
downcast, without a reference count change.
See also crate::Any::is.