anypointer

Module

Implement a generic unsafe pointer type.

You can import these APIs from the memory package. For example:

from memory.anypointer import AnyPointer

AnyPointer

This is a pointer type that can point to any generic value that is movable.

Parameters:

  • ​T (Movable): The pointer element type, which must be movable.

Aliases:

  • ​pointer_type = pointer<:trait<@"$stdlib"::@"$builtin"::@"$value"::@Movable> T>: The underlying pointer type.

Fields:

  • ​value (pointer<:trait<@"$stdlib"::@"$builtin"::@"$value"::@Movable> T>): The underlying pointer.

Implemented traits:

AnyType

Methods:

__init__

__init__() -> Self

Create a null pointer.

Returns:

A null pointer.

__bool__

__bool__(self: Self) -> Bool

Return true if the pointer is non-null.

Returns:

Whether the pointer is null.

__add__

__add__(self: Self, offset: Int) -> Self

Return a pointer at an offset from the current one.

Args:

  • ​offset (Int): The offset index.

Returns:

An offset pointer.

alloc

static alloc(count: Int) -> Self

Allocate an array with default alignment.

Args:

  • ​count (Int): The number of elements in the array.

Returns:

The pointer to the newly allocated array.

free

free(self: Self)

Free the memory referenced by the pointer.

take_value

take_value(self: Self) -> T

Move the value at the pointer out.

Returns:

The value at the pointer.

emplace_value

emplace_value(self: Self, owned value: T)

Emplace a new value into the pointer location.

Args:

  • ​value (T): The value to emplace.