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:
StructRefProxyThe Python object for an
AnyTypevalue, a structref that holds one value of any numba type.Create one with
make_any(). Calling the class raisesNotImplementedErrorin Python, and jitted code that calls it fails to compile.
- class numbox.core.any.any_type.AnyTypeClass(*args, **kwargs)[source]
Bases:
StructRefThe numba type class of
AnyType, the structref type of anAny, with one fieldpofErasedType.
- numbox.core.any.any_type.make_any(x)[source]
Return a new
Anythat holdsx; 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 typety, which is not checked.
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.
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.