Skip to main content
Log in

Mojo struct

List

Uncounted reference to underlying storage of a list Value.

The user must take special care not to allow the underlying Value from which this list was obtained to be destroyed prior to being done with the List. However, items within the list are separately reference-counted, so it is safe to use an item retrieved from the list after the list itself has been destroyed, or to destroy an item that was appended to a list (provided, however, that if the item itself is borrowing data (e.g. a tensor), that that underlying data must remain present).

Implemented traits

AnyType, Sized

Methods

__init__

__init__(inout self: Self, ptr: CList, lib: DLHandle, owned session: InferenceSession)

Internal use only.

To create lists yourself, use InferenceSession.new_list_value and then use Value.as_list on the resulting value.

Args:

  • ptr (CList): Internal use only.
  • lib (DLHandle): Internal use only.
  • session (InferenceSession): Internal use only.

__moveinit__

__moveinit__(inout self: Self, owned existing: Self)

Create a new List pointing at the internals of another List.

Lists do not own anything and are borrowed from the internal storage of a Value, so the user must continue to take care to ensure that the Value the original List was sourced from continues to outlive this new List object.

Args:

  • existing (Self): The List to represent.

__del__

__del__(owned self: Self)

Release the handle to this list.

The underlying storage remains owned by the Value from which this List was obtained.

__getitem__

__getitem__(self: Self, index: Int) -> Value

Get the value at an index of the list.

The returned Value owns a new reference to the underlying storage of the value, and so the returned item Value may be safely used even after the list (and value containing the list) are destroyed.

Args:

  • index (Int): The index of the item to retrieve within the list.

Returns:

A new reference to an existing value within the list.

Raises:

If the index is out of bounds.

__len__

__len__(self: Self) -> Int

Get the length of the list.

Returns:

The length of the list.

append

append(self: Self, value: Value)

Append a Value to the list.

The list will own a new reference to the value, so it is safe to allow value to be destroyed after this operation.

Args:

  • value (Value): The value to append to the list.

Was this page helpful?