numbox.core.any

Overview

Implementation of Any type counterpart of C++ std::any leveraging the type-erasure technique.

Modules

numbox.core.any.any_type

Any, a structref that holds one value of any numba type, the counterpart of C++’s std::any.

make_any() wraps a value in a structref of the value’s own type, casts that structref to ErasedType and stores it in the field p of a new AnyType value. Every Any therefore has the same numba type, regardless of the value that it holds. get_as(ty) reads the value back as the type ty, and reset(x) replaces it with x. get_as does not check ty: reading a value as another type reinterprets its bytes.

class numbox.core.any.any_type.Any(x)[source]

Bases: StructRefProxy

The Python object for an AnyType value, a structref that holds one value of any numba type.

Create one with make_any(). Calling the class raises NotImplementedError in Python, and jitted code that calls it fails to compile.

get_as(ty)[source]

Return the value that the Any holds, read as the numba type ty.

ty must be the type of the stored value. The type is not checked, and reading the value as another type reinterprets its bytes.

reset(val)[source]

Replace the value that the Any holds with val, which can be of another type.

class numbox.core.any.any_type.AnyTypeClass(*args, **kwargs)[source]

Bases: StructRef

The numba type class of AnyType, the structref type of an Any, with one field p of ErasedType.

numbox.core.any.any_type.make_any(x)[source]

Return a new Any that holds x; Python code and jitted code can call it.

numbox.core.any.any_type.ol_get_as(self_ty, ty_ref: TypeRef)[source]

Implement Any.get_as(ty) for jitted code: read the stored value as the type ty, which is not checked.

numbox.core.any.any_type.ol_make_any(x_ty)[source]

Implement _make_any(x) for jitted code: return a new Any that holds x.

numbox.core.any.any_type.ol_reset(self_ty, x_ty)[source]

Implement Any.reset(x) for jitted code: store a structref that holds x in p, as ErasedType.

numbox.core.any.content_wrap

A wrapper that puts a value of any numba type in a structref of the value’s own type, for any_type.

In jitted code, _Content(x) returns a new structref with one field, x, that holds the value. Its type is ContentTypeClass([("x", <the type of x>)]), so values of different types are wrapped in structrefs of different types. numbox.core.any.any_type casts that structref to ErasedType to store it in an Any. Only jitted code can call _Content with a value.

class numbox.core.any.content_wrap.ContentTypeClass(*args, **kwargs)[source]

Bases: StructRef

The numba type class of the structref that _Content returns, with one field x of the value’s type.

numbox.core.any.content_wrap.ol_content(x_ty)[source]

Implement _Content(x) for jitted code: return a new structref whose field x holds x.

numbox.core.any.erased_type

ErasedType, a structref type with no fields that holds the value of an Any.

numbox.core.any.any_type wraps each value in a structref of the value’s own type and casts that structref to ErasedType, so that every Any has the same numba type. numbox defines no boxing for ErasedType, so jitted code cannot return a value of this type to Python.

class numbox.core.any.erased_type.ErasedTypeClass(*args, **kwargs)[source]

Bases: StructRef

The numba type class of ErasedType, a structref type with no fields.