Skip to main content
Log in

Mojo struct

Arc

Atomic reference-counted pointer.

This smart pointer owns an instance of T indirectly managed on the heap. This pointer is copyable, including across threads, maintaining a reference count to the underlying data.

This pointer itself is thread-safe using atomic accesses to reference count the underlying data, but references returned to the underlying data are not thread safe.

Parameters

  • T (Movable): The type of the stored value.

Implemented traits

AnyType, CollectionElement, CollectionElementNew, Copyable, ExplicitlyCopyable, Movable

Methods

__init__

__init__(inout self: Self, owned value: T)

Construct a new thread-safe, reference-counted smart pointer, and move the value into heap memory managed by the new pointer.

Args:

  • value (T): The value to manage.

__init__(inout self: Self, *, other: Self)

Copy the object.

Args:

  • other (Self): The value to copy.

__copyinit__

__copyinit__(inout self: Self, existing: Self)

Copy an existing reference. Increment the refcount to the object.

Args:

  • existing (Self): The existing reference.

__del__

__del__(owned self: Self)

Delete the smart pointer reference.

Decrement the ref count for the reference. If there are no more references, delete the object and free its memory.

__getitem__

__getitem__[self_life: ImmutableLifetime](ref [self_life] self: Self) -> ref [(mutcast imm *(0,0))] T

Returns a mutable Reference to the managed value.

Parameters:

  • self_life (ImmutableLifetime): The lifetime of self.

Returns:

A Reference to the managed value.

unsafe_ptr

unsafe_ptr(self: Self) -> UnsafePointer[T, 0, 0, alignof[::AnyType,__mlir_type.!kgen.target]() if triple_is_nvidia_cuda() else 1]

Retrieves a pointer to the underlying memory.

Returns:

The UnsafePointer to the underlying memory.

Was this page helpful?