Skip to main content

Array

Struct Array 

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

Source

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]));
Source

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>

Source

pub fn new() -> Self

Create an empty array.

§Returns

The empty array.

See also Array::from_vec.

Source

pub fn from_vec(items: Vec<T>) -> Self

Create an array holding items, like Array::from(items).

§Arguments
  • items - The elements.
§Returns

The array.

§Examples
use tvm_ffi::Array;

let array = Array::from_vec(vec![1_i64, 2, 3]);
assert_eq!(array[1], 2);
Source

pub fn push(&mut self, value: T)

Append value to the end.

See also List::push.

Source

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.

Source

pub fn pop(&mut self) -> Option<T>

Remove and return the last element, or None if the array is empty, as Vec::pop.

See also List::pop.

Source

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.

Source

pub fn set(&mut self, index: usize, value: T)

Replace the element at index, as vec[index] = value.

§Panics

Panics if index >= len.

See also List::set.

Source

pub fn len(&self) -> usize

Return the number of elements.

§Returns

The number of elements.

Source

pub fn is_empty(&self) -> bool

Return true if the array has no elements.

§Returns

true if the array has no elements.

Source

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>

Source

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>

Source§

unsafe fn is_undefined(field: *const Self) -> bool

Whether 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 more
Source§

fn type_str() -> String

The type name in conversion errors and type schemas, such as int, ffi.String or Array<int>.
Source§

fn type_schema() -> String

The type schema of the type: the JSON object that describes it, which the type schemas of functions and fields embed. Read more
Source§

unsafe fn check_any_strict(data: &TVMFFIAny) -> bool

Whether data holds a Self as is, without conversion. Read more
Source§

unsafe fn copy_to_any_view(src: &Self, data: &mut TVMFFIAny)

Write a cell that borrows src into data. Read more
Source§

unsafe fn move_to_any(src: Self, data: &mut TVMFFIAny)

Write a cell that owns src into data. Read more
Source§

unsafe fn copy_from_any_view_after_check(data: &TVMFFIAny) -> Self

Copy a Self out of a cell that holds one, retaining any object. Read more
Source§

unsafe fn move_from_any_after_check(data: &mut TVMFFIAny) -> Self

Move a Self out of an owning cell that holds one. Read more
Source§

unsafe fn try_cast_from_any_view(data: &TVMFFIAny) -> Result<Self, ()>

Convert data into a Self by the FFI rules, such as an integer into a float; Err if it does not convert. Read more
Source§

fn get_mismatch_type_info(data: &TVMFFIAny) -> String

The name of the mismatched value data in conversion errors; by default its runtime type key.
Source§

fn to_any(&self) -> Any

Borrow self into an owned Any, increfing object-backed values. Read more
Source§

impl<T: Clone> Clone for Array<T>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<T: ContainerElement + Clone> Debug for Array<T>

Source§

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

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

impl<T: ContainerElement + Clone> Default for Array<T>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<T: ContainerElement + Clone> Display for Array<T>

Formats the array as its repr; see AnyView.

Source§

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

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

impl<T> DowncastRefFromAny for Array<T>

Source§

unsafe fn downcast_ref_from_any(cell: &TVMFFIAny) -> Option<&Self>

Borrow cell as Self when it holds one. Read more
Source§

impl<T: ContainerElement + Clone> From<&Array<T>> for Array<T>

A reference-count bump, as From<&T> for T of an object reference.

Source§

fn from(value: &Array<T>) -> Self

Converts to this type from the input type.
Source§

impl<T: ContainerElement + Clone> From<&[T]> for Array<T>

Source§

fn from(items: &[T]) -> Self

Converts to this type from the input type.
Source§

impl<T: ContainerElement + Clone> From<Array<T>> for ObjectRef

An array upcasts to the root ObjectRef.

Source§

fn from(value: Array<T>) -> Self

Converts to this type from the input type.
Source§

impl<T: ContainerElement + Clone> From<Vec<T>> for Array<T>

Source§

fn from(items: Vec<T>) -> Self

Converts to this type from the input type.
Source§

impl<T: ContainerElement + Clone, const N: usize> From<[T; N]> for Array<T>

Source§

fn from(items: [T; N]) -> Self

Converts to this type from the input type.
Source§

impl<T: ContainerElement + Clone> FromIterator<T> for Array<T>

Source§

fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self

Creates a value from an iterator. Read more
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.

Source§

type Output = T

The returned type after indexing.
Source§

fn index(&self, index: usize) -> &T

Performs the indexing (container[index]) operation. Read more
Source§

impl<T, U: TypeSubsumes<T>> IntoArg<Array<U>> for Array<T>

Source§

type Output = Array<U>

T when owned, &T when borrowed.
Source§

fn into_arg(self) -> Array<U>

Convert into a T or a &T.
Source§

fn into_owned(self) -> Array<U>

Convert into an owned T, cloning only a borrowed value.
Source§

impl<'a, T, U: TypeSubsumes<T> + ContainerElement + Clone + 'a> IntoArg<Array<U>> for &'a Array<T>

Source§

type Output = &'a Array<U>

T when owned, &T when borrowed.
Source§

fn into_arg(self) -> &'a Array<U>

Convert into a T or a &T.
Source§

fn into_owned(self) -> Array<U>

Convert into an owned T, cloning only a borrowed value.
Source§

impl<'a, T: ContainerElement + Clone + RefFromAnyUnchecked> IntoIterator for &'a Array<T>

Source§

type Item = &'a T

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a, T>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
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.

Source§

type Item = T

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<T>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<T: ContainerElement + Clone> ObjectRefCore for Array<T>

Source§

type ContainerType = ArrayObj

The object type the reference points to.
Source§

fn data(this: &Self) -> &ObjectArc<Self::ContainerType>

Borrow the object’s handle.
Source§

fn into_data(this: Self) -> ObjectArc<Self::ContainerType>

Take the object’s handle.
Source§

unsafe fn from_data(data: ObjectArc<Self::ContainerType>) -> Self

Make a reference from the object’s handle. Read more
Source§

fn same_as<Other: ObjectRefCore>(&self, other: &Other) -> bool

Whether two references point to the same object.
Source§

fn type_key(&self) -> &'static str

The registered type key of the object’s runtime type, such as "ffi.Array", as C++ ObjectRef::GetTypeKey.
Source§

fn is<T: AnyCompatible>(&self) -> bool

Whether the reference is a 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.
Source§

fn downcast_ref<T: DowncastRefFromObject>(&self) -> Option<&T>

Borrow the underlying object as T when it is one. Read more
Source§

fn downcast<B>(self) -> Result<B, Self>
where B: ObjectRefCore,

Take self as B, a subtype or a container such as Array<i64> whose elements are checked too, when its object is one, without copying it. On a mismatch the reference is returned unchanged. Read more
Source§

impl<T: ContainerElement + Clone> PackedCallArg for Array<T>

Source§

type Holder = Array<T>

What keeps the argument alive during the call.
Source§

fn into_packed_arg(self) -> Array<T>

Source§

fn as_packed_arg(holder: &Self::Holder) -> AnyView<'_>

Source§

impl<'a, T: ContainerElement + Clone> PackedCallArg for &'a Array<T>

Source§

type Holder = &'a Array<T>

What keeps the argument alive during the call.
Source§

fn into_packed_arg(self) -> &'a Array<T>

Source§

fn as_packed_arg(holder: &Self::Holder) -> AnyView<'_>

Source§

impl<T: Send + Sync> Send for Array<T>

Source§

impl<T: Send + Sync> Sync for Array<T>

Source§

impl<'b, T> TryFrom<&'b Any> for Array<T>

Source§

type Error = Error

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

fn try_from(value: &'b Any) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl<T: ContainerElement + Clone> TryFrom<&ObjectRef> for Array<T>

Source§

type Error = Error

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

fn try_from(value: &ObjectRef) -> Result<Self>

Performs the conversion.
Source§

impl<T> TryFrom<Any> for Array<T>

Source§

type Error = Error

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

fn try_from(value: Any) -> 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<T: ContainerElement + Clone> TryFrom<ObjectRef> for Array<T>

The strict downcast from the root ObjectRef.

Source§

type Error = Error

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

fn try_from(value: ObjectRef) -> Result<Self>

Performs the conversion.

Auto Trait Implementations§

§

impl<T> Forget for Array<T>

§

impl<T> Freeze for Array<T>
where PhantomData<T>: Freeze,

§

impl<T> Move for Array<T>

§

impl<T> RefUnwindSafe for Array<T>

§

impl<T> Unpin for Array<T>
where PhantomData<T>: Unpin,

§

impl<T> UnsafeUnpin for Array<T>

§

impl<T> UnwindSafe for Array<T>

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> ContainerElement for T
where T: AnyCompatible,

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 = !

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

fn try_from(value: U) -> Result<T, !>

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,