Mojo struct
UnsafePointerV2
@register_passable(trivial)
struct UnsafePointerV2[mut: Bool, //, type: AnyType, origin: Origin[mut], *, address_space: AddressSpace = AddressSpace(0)]
UnsafePointerV2[T] represents an indirect reference to one or more values of type T consecutively in memory, and can refer to uninitialized memory.
Because it supports referring to uninitialized memory, it provides unsafe
methods for initializing and destroying instances of T, as well as methods
for accessing the values once they are initialized. You should instead use
safer pointers when possible.
Differences from UnsafePointer (V1):
UnsafePointerV2fixes the unsafe implicit mutability and origin casting issues ofUnsafePointer.UnsafePointerV2has an inferred mutability parameter.UnsafePointerV2does not have a defaulted origin parameter, this must be explicitly specified or unbound.
Important things to know:
- This pointer is unsafe and nullable. No bounds checks; reading before writing is undefined.
- It does not own existing memory. When memory is heap-allocated with
alloc(), you must callfree(). - For simple read/write access, use
(ptr + i)[]orptr[i]whereiis the offset size.
For more information see Unsafe pointers in the Mojo Manual. For a comparison with other pointer types, see Intro to pointers.
Parameters
- mut (
Bool): Whether the origin is mutable. - type (
AnyType): The type the pointer points to. - origin (
Origin): The origin of the memory being addressed. - address_space (
AddressSpace): The address space associated with the UnsafePointer allocated memory.
Fields
- address (
pointer<:trait<@stdlib::@builtin::@anytype::@AnyType> *"type", #lit.struct.extract<:@stdlib::@builtin::@int::@Int #lit.struct.extract<:@stdlib::@memory::@pointer::@AddressSpace address_space, "_value">, "_mlir_value">>): The underlying pointer.
Implemented traits
AnyType,
Boolable,
Copyable,
Defaultable,
EqualityComparable,
GreaterThanComparable,
GreaterThanOrEqualComparable,
ImplicitlyBoolable,
ImplicitlyCopyable,
Intable,
LessThanComparable,
LessThanOrEqualComparable,
Movable,
Stringable,
UnknownDestructibility,
Writable
Aliases
__copyinit__is_trivial
alias __copyinit__is_trivial = True
__del__is_trivial
alias __del__is_trivial = True
__moveinit__is_trivial
alias __moveinit__is_trivial = True
Methods
__init__
__init__() -> Self
Create a null pointer.
__init__(*, ref [origin, address_space] to: type) -> Self
Constructs a Pointer from a reference to a value.
Args:
- to (
type): The value to construct a pointer to.
@implicit
__init__(other: UnsafePointerV2[type, origin, address_space=address_space]) -> UnsafePointerV2[type, origin_of((muttoimm origin._mlir_origin)), address_space=address_space]
Implicitly casts a mutable pointer to immutable.
Args:
- other (
UnsafePointerV2): The mutable pointer to cast from.
Returns:
@implicit
__init__(other: UnsafePointerV2[type, origin, address_space=address_space]) -> UnsafePointerV2[type, MutableAnyOrigin, address_space=address_space]
Implicitly casts a mutable pointer to MutableAnyOrigin.
Args:
- other (
UnsafePointerV2): The mutable pointer to cast from.
Returns:
@implicit
__init__(other: UnsafePointerV2[type, origin, address_space=address_space]) -> UnsafePointerV2[type, ImmutableAnyOrigin, address_space=address_space]
Implicitly casts a pointer to ImmutableAnyOrigin.
Args:
- other (
UnsafePointerV2): The pointer to cast from.
Returns:
__init__(*, ref [origin] unchecked_downcast_value: PythonObject) -> UnsafePointerV2[type, origin]
Downcast a PythonObject known to contain a Mojo object to a pointer.
This operation is only valid if the provided Python object contains an initialized Mojo object of matching type.
Args:
- unchecked_downcast_value (
PythonObject): The Python object to downcast from.
Returns:
__bool__
__bool__(self) -> Bool
Return true if the pointer is non-null.
Returns:
Bool: Whether the pointer is null.
__getitem__
__getitem__(self) -> ref [origin, address_space] type
Return a reference to the underlying data.
Safety: The pointer must not be null and must point to initialized memory.
Returns:
ref: A reference to the value.
__getitem__[I: Indexer, //](self, offset: I) -> ref [origin, address_space] type
Return a reference to the underlying data, offset by the given index.
Parameters:
- I (
Indexer): A type that can be used as an index.
Args:
- offset (
I): The offset index.
Returns:
ref: An offset reference.
__lt__
__lt__(self, rhs: UnsafePointerV2[type, origin, address_space=address_space]) -> Bool
Returns True if this pointer represents a lower address than rhs.
Args:
- rhs (
UnsafePointerV2): The value of the other pointer.
Returns:
Bool: True if this pointer represents a lower address and False otherwise.
__lt__(self, rhs: Self) -> Bool
Returns True if this pointer represents a lower address than rhs.
Args:
- rhs (
Self): The value of the other pointer.
Returns:
Bool: True if this pointer represents a lower address and False otherwise.
__le__
__le__(self, rhs: UnsafePointerV2[type, origin, address_space=address_space]) -> Bool
Returns True if this pointer represents a lower than or equal address than rhs.
Args:
- rhs (
UnsafePointerV2): The value of the other pointer.
Returns:
Bool: True if this pointer represents a lower address and False otherwise.
__le__(self, rhs: Self) -> Bool
Returns True if this pointer represents a lower than or equal address than rhs.
Args:
- rhs (
Self): The value of the other pointer.
Returns:
Bool: True if this pointer represents a lower address and False otherwise.
__eq__
__eq__(self, rhs: UnsafePointerV2[type, origin, address_space=address_space]) -> Bool
Returns True if the two pointers are equal.
Args:
- rhs (
UnsafePointerV2): The value of the other pointer.
Returns:
Bool: True if the two pointers are equal and False otherwise.
__eq__(self, rhs: Self) -> Bool
Returns True if the two pointers are equal.
Args:
- rhs (
Self): The value of the other pointer.
Returns:
Bool: True if the two pointers are equal and False otherwise.
__ne__
__ne__(self, rhs: UnsafePointerV2[type, origin, address_space=address_space]) -> Bool
Returns True if the two pointers are not equal.
Args:
- rhs (
UnsafePointerV2): The value of the other pointer.
Returns:
Bool: True if the two pointers are not equal and False otherwise.
__ne__(self, rhs: Self) -> Bool
Returns True if the two pointers are not equal.
Args:
- rhs (
Self): The value of the other pointer.
Returns:
Bool: True if the two pointers are not equal and False otherwise.
__gt__
__gt__(self, rhs: UnsafePointerV2[type, origin, address_space=address_space]) -> Bool
Returns True if this pointer represents a higher address than rhs.
Args:
- rhs (
UnsafePointerV2): The value of the other pointer.
Returns:
Bool: True if this pointer represents a higher than or equal address and
False otherwise.
__gt__(self, rhs: Self) -> Bool
Returns True if this pointer represents a higher address than rhs.
Args:
- rhs (
Self): The value of the other pointer.
Returns:
Bool: True if this pointer represents a higher than or equal address and
False otherwise.
__ge__
__ge__(self, rhs: UnsafePointerV2[type, origin, address_space=address_space]) -> Bool
Returns True if this pointer represents a higher than or equal address than rhs.
Args:
- rhs (
UnsafePointerV2): The value of the other pointer.
Returns:
Bool: True if this pointer represents a higher than or equal address and
False otherwise.
__ge__(self, rhs: Self) -> Bool
Returns True if this pointer represents a higher than or equal address than rhs.
Args:
- rhs (
Self): The value of the other pointer.
Returns:
Bool: True if this pointer represents a higher than or equal address and
False otherwise.
__add__
__add__[I: Indexer, //](self, offset: I) -> Self
Return a pointer at an offset from the current one.
Parameters:
- I (
Indexer): A type that can be used as an index.
Args:
- offset (
I): The offset index.
Returns:
Self: An offset pointer.
__sub__
__sub__[I: Indexer, //](self, offset: I) -> Self
Return a pointer at an offset from the current one.
Parameters:
- I (
Indexer): A type that can be used as an index.
Args:
- offset (
I): The offset index.
Returns:
Self: An offset pointer.
__iadd__
__iadd__[I: Indexer, //](mut self, offset: I)
Add an offset to this pointer.
Parameters:
- I (
Indexer): A type that can be used as an index.
Args:
- offset (
I): The offset index.
__isub__
__isub__[I: Indexer, //](mut self, offset: I)
Subtract an offset from this pointer.
Parameters:
- I (
Indexer): A type that can be used as an index.
Args:
- offset (
I): The offset index.
offset
offset[I: Indexer, //](self, idx: I) -> Self
Returns a new pointer shifted by the specified offset.
Parameters:
- I (
Indexer): A type that can be used as an index.
Args:
- idx (
I): The offset of the new pointer.
Returns:
Self: The new constructed UnsafePointer.
__merge_with__
__merge_with__[other_type: AnyStruct[UnsafePointerV2[type, origin, address_space=address_space]]](self) -> UnsafePointerV2[type, origin_of((mutcast origin._mlir_origin), (mutcast origin._mlir_origin)), address_space=address_space]
Returns a pointer merged with the specified other_type.
Parameters:
- other_type (
AnyStruct): The type of the pointer to merge with.
Returns:
UnsafePointerV2: A pointer merged with the specified other_type.
__int__
__int__(self) -> Int
Returns the pointer address as an integer.
Returns:
Int: The address of the pointer as an Int.
__str__
__str__(self) -> String
Gets a string representation of the pointer.
Returns:
String: The string representation of the pointer.
write_to
write_to(self, mut writer: T)
Formats this pointer address to the provided Writer.
Args:
- writer (
T): The object to write to.
swap_pointees
swap_pointees[U: Movable, //](self: UnsafePointerV2[U, origin], other: UnsafePointerV2[U, origin])
Swap the values at the pointers.
This function assumes that self and other may overlap in memory.
If that is not the case, or when references are available, you should
use builtin.swap instead.
Safety:
selfandothermust both point to valid, initialized instances ofT.
Parameters:
- U (
Movable): The type the pointers point to, which must beMovable.
Args:
- other (
UnsafePointerV2): The other pointer to swap with.
as_noalias_ptr
as_noalias_ptr(self) -> Self
Cast the pointer to a new pointer that is known not to locally alias any other pointer. In other words, the pointer transitively does not alias any other memory value declared in the local function context.
This information is relayed to the optimizer. If the pointer does locally alias another memory value, the behaviour is undefined.
Returns:
Self: A noalias pointer.
load
load[dtype: DType, //, width: Int = 1, *, alignment: Int = align_of[dtype](), volatile: Bool = False, invariant: Bool = _default_invariant[mut]()](self: UnsafePointerV2[Scalar[dtype], origin, address_space=address_space]) -> SIMD[dtype, width]
Loads width elements from the value the pointer points to.
Use alignment to specify minimal known alignment in bytes; pass a
smaller value (such as 1) if loading from packed/unaligned memory. The
volatile/invariant flags control reordering and common-subexpression
elimination semantics for special cases.
Example:
var p = UnsafePointer[Int32].alloc(8)
p.store(0, SIMD[DType.int32, 4](1, 2, 3, 4))
var v = p.load[width=4]()
print(v) # => [1, 2, 3, 4]
p.free()Constraints:
The width and alignment must be positive integer values.
Parameters:
- dtype (
DType): The data type of the SIMD vector. - width (
Int): The number of elements to load. - alignment (
Int): The minimal alignment (bytes) of the address. - volatile (
Bool): Whether the operation is volatile. - invariant (
Bool): Whether the load is from invariant memory.
Returns:
SIMD: The loaded SIMD vector.
load[dtype: DType, //, width: Int = 1, *, alignment: Int = align_of[dtype](), volatile: Bool = False, invariant: Bool = _default_invariant[mut]()](self: UnsafePointerV2[Scalar[dtype], origin, address_space=address_space], offset: Scalar[dtype]) -> SIMD[dtype, width]
Loads the value the pointer points to with the given offset.
Constraints:
The width and alignment must be positive integer values. The offset must be integer.
Parameters:
- dtype (
DType): The data type of SIMD vector elements. - width (
Int): The size of the SIMD vector. - alignment (
Int): The minimal alignment of the address. - volatile (
Bool): Whether the operation is volatile or not. - invariant (
Bool): Whether the memory is load invariant.
Args:
- offset (
Scalar): The offset to load from.
Returns:
SIMD: The loaded value.
load[I: Indexer, dtype: DType, //, width: Int = 1, *, alignment: Int = align_of[dtype](), volatile: Bool = False, invariant: Bool = _default_invariant[mut]()](self: UnsafePointerV2[Scalar[dtype], origin, address_space=address_space], offset: I) -> SIMD[dtype, width]
Loads the value the pointer points to with the given offset.
Constraints:
The width and alignment must be positive integer values.
Parameters:
- I (
Indexer): A type that can be used as an index. - dtype (
DType): The data type of SIMD vector elements. - width (
Int): The size of the SIMD vector. - alignment (
Int): The minimal alignment of the address. - volatile (
Bool): Whether the operation is volatile or not. - invariant (
Bool): Whether the memory is load invariant.
Args:
- offset (
I): The offset to load from.
Returns:
SIMD: The loaded value.
store
store[I: Indexer, dtype: DType, //, width: Int = 1, *, alignment: Int = align_of[dtype](), volatile: Bool = False](self: UnsafePointerV2[Scalar[dtype], origin, address_space=address_space], offset: I, val: SIMD[dtype, width])
Stores a single element value at the given offset.
Constraints:
The width and alignment must be positive integer values. The offset must be integer.
Parameters:
- I (
Indexer): A type that can be used as an index. - dtype (
DType): The data type of SIMD vector elements. - width (
Int): The size of the SIMD vector. - alignment (
Int): The minimal alignment of the address. - volatile (
Bool): Whether the operation is volatile or not.
Args:
- offset (
I): The offset to store to. - val (
SIMD): The value to store.
store[dtype: DType, offset_type: DType, //, width: Int = 1, *, alignment: Int = align_of[dtype](), volatile: Bool = False](self: UnsafePointerV2[Scalar[dtype], origin, address_space=address_space], offset: Scalar[offset_type], val: SIMD[dtype, width])
Stores a single element value at the given offset.
Constraints:
The width and alignment must be positive integer values.
Parameters:
- dtype (
DType): The data type of SIMD vector elements. - offset_type (
DType): The data type of the offset value. - width (
Int): The size of the SIMD vector. - alignment (
Int): The minimal alignment of the address. - volatile (
Bool): Whether the operation is volatile or not.
Args:
store[dtype: DType, //, width: Int = 1, *, alignment: Int = align_of[dtype](), volatile: Bool = False](self: UnsafePointerV2[Scalar[dtype], origin, address_space=address_space], val: SIMD[dtype, width])
Stores a single element value val at element offset 0.
Specify alignment when writing to packed/unaligned memory. Requires a
mutable pointer. For writing at an element offset, use the overloads
that accept an index or scalar offset.
Example:
var p = UnsafePointer[Float32].alloc(4)
var vec = SIMD[DType.float32, 4](1.0, 2.0, 3.0, 4.0)
p.store(vec)
var out = p.load[width=4]()
print(out) # => [1.0, 2.0, 3.0, 4.0]
p.free()Constraints:
The width and alignment must be positive integer values.
Parameters:
- dtype (
DType): The data type of SIMD vector elements. - width (
Int): The number of elements to store. - alignment (
Int): The minimal alignment (bytes) of the address. - volatile (
Bool): Whether the operation is volatile.
Args:
- val (
SIMD): The SIMD value to store.
strided_load
strided_load[dtype: DType, T: Intable, //, width: Int](self: UnsafePointerV2[Scalar[dtype], origin, address_space=address_space], stride: T) -> SIMD[dtype, width]
Performs a strided load of the SIMD vector.
Parameters:
- dtype (
DType): DType of returned SIMD value. - T (
Intable): The Intable type of the stride. - width (
Int): The SIMD width.
Args:
- stride (
T): The stride between loads.
Returns:
SIMD: A vector which is stride loaded.
strided_store
strided_store[dtype: DType, T: Intable, //, width: Int = 1](self: UnsafePointerV2[Scalar[dtype], origin, address_space=address_space], val: SIMD[dtype, width], stride: T)
Performs a strided store of the SIMD vector.
Parameters:
- dtype (
DType): DType ofval, the SIMD value to store. - T (
Intable): The Intable type of the stride. - width (
Int): The SIMD width.
Args:
- val (
SIMD): The SIMD value to store. - stride (
T): The stride between stores.
gather
gather[dtype: DType, //, *, width: Int = 1, alignment: Int = align_of[dtype]()](self: UnsafePointerV2[Scalar[dtype], origin, address_space=address_space], offset: SIMD[dtype, width], mask: SIMD[DType.bool, width] = SIMD[DType.bool, width](True), default: SIMD[dtype, width] = 0) -> SIMD[dtype, width]
Gathers a SIMD vector from offsets of the current pointer.
This method loads from memory addresses calculated by appropriately
shifting the current pointer according to the offset SIMD vector,
or takes from the default SIMD vector, depending on the values of
the mask SIMD vector.
If a mask element is True, the respective result element is given
by the current pointer and the offset SIMD vector; otherwise, the
result element is taken from the default SIMD vector.
Constraints:
The offset type must be an integral type. The alignment must be a power of two integer value.
Parameters:
- dtype (
DType): DType of the return SIMD. - width (
Int): The SIMD width. - alignment (
Int): The minimal alignment of the address.
Args:
- offset (
SIMD): The SIMD vector of offsets to gather from. - mask (
SIMD): The SIMD vector of boolean values, indicating for each element whether to load from memory or to take from thedefaultSIMD vector. - default (
SIMD): The SIMD vector providing default values to be taken where themaskSIMD vector isFalse.
Returns:
SIMD: The SIMD vector containing the gathered values.
scatter
scatter[dtype: DType, //, *, width: Int = 1, alignment: Int = align_of[dtype]()](self: UnsafePointerV2[Scalar[dtype], origin, address_space=address_space], offset: SIMD[dtype, width], val: SIMD[dtype, width], mask: SIMD[DType.bool, width] = SIMD[DType.bool, width](True))
Scatters a SIMD vector into offsets of the current pointer.
This method stores at memory addresses calculated by appropriately
shifting the current pointer according to the offset SIMD vector,
depending on the values of the mask SIMD vector.
If a mask element is True, the respective element in the val SIMD
vector is stored at the memory address defined by the current pointer
and the offset SIMD vector; otherwise, no action is taken for that
element in val.
If the same offset is targeted multiple times, the values are stored
in the order they appear in the val SIMD vector, from the first to
the last element.
Constraints:
The offset type must be an integral type. The alignment must be a power of two integer value.
Parameters:
- dtype (
DType): DType ofvalue, the result SIMD buffer. - width (
Int): The SIMD width. - alignment (
Int): The minimal alignment of the address.
Args:
free
free(self: UnsafePointerV2[type, origin])
Free the memory referenced by the pointer.
bitcast
bitcast[T: AnyType](self) -> UnsafePointerV2[T, origin, address_space=address_space]
Bitcasts an UnsafePointer to a different type.
Parameters:
- T (
AnyType): The target type.
Returns:
UnsafePointerV2: A new UnsafePointer object with the specified type and the same
address, mutability, and origin as the original UnsafePointer.
mut_cast
mut_cast[target_mut: Bool](self) -> UnsafePointerV2[type, origin_of((mutcast origin._mlir_origin)), address_space=address_space]
Changes the mutability of a pointer.
This is a safe way to change the mutability of a pointer with an unbounded mutability. This function will emit a compile time error if you try to cast an immutable pointer to mutable.
Parameters:
- target_mut (
Bool): Mutability of the destination pointer.
Returns:
UnsafePointerV2: A pointer with the same type, origin and address space as the
original pointer, but with the newly specified mutability.
unsafe_mut_cast
unsafe_mut_cast[target_mut: Bool](self) -> UnsafePointerV2[type, origin_of((mutcast origin._mlir_origin)), address_space=address_space]
Changes the mutability of a pointer.
If you are unconditionally casting the mutability to False, use
as_immutable instead.
If you are casting to mutable or a parameterized mutability, prefer
using the safe mut_cast method instead.
Safety:
Casting the mutability of a pointer is inherently very unsafe.
Improper usage can lead to undefined behavior. Consider restricting
types to their proper mutability at the function signature level.
For example, taking an UnsafeMutPointer[T, **_] as an
argument over an unbound UnsafePointer[T, **_] is preferred.
Parameters:
- target_mut (
Bool): Mutability of the destination pointer.
Returns:
UnsafePointerV2: A pointer with the same type, origin and address space as the
original pointer, but with the newly specified mutability.
unsafe_origin_cast
unsafe_origin_cast[target_origin: Origin[mut]](self) -> UnsafePointerV2[type, target_origin, address_space=address_space]
Changes the origin of a pointer.
If you are unconditionally casting the origin to an AnyOrigin, use
as_any_origin instead.
Safety: Casting the origin of a pointer is inherently very unsafe. Improper usage can lead to undefined behavior or unexpected variable destruction. Considering parameterizing the origin at the function level to avoid unnecessary casts.
Parameters:
- target_origin (
Origin): Origin of the destination pointer.
Returns:
UnsafePointerV2: A pointer with the same type, mutability and address space as the
original pointer, but with the newly specified origin.
as_immutable
as_immutable(self) -> UnsafePointerV2[type, origin_of((muttoimm origin._mlir_origin)), address_space=address_space]
Changes the mutability of a pointer to immutable.
Unlike unsafe_mut_cast, this function is always safe to use as casting
from (im)mutable to immutable is always safe.
Returns:
UnsafePointerV2: A pointer with the mutability set to immutable.
as_any_origin
as_any_origin(self: UnsafePointerV2[type, origin, address_space=address_space]) -> UnsafePointerV2[type, ImmutableAnyOrigin, address_space=address_space]
Casts the origin of an immutable pointer to ImmutableAnyOrigin.
It is usually preferred to maintain concrete origin values instead of
using ImmutableAnyOrigin. However, if it is needed, keep in mind that
ImmutableAnyOrigin can alias any memory value, so Mojo's ASAP
destruction will not apply during the lifetime of the pointer.
Returns:
UnsafePointerV2: A pointer with the origin set to ImmutableAnyOrigin.
as_any_origin(self: UnsafePointerV2[type, origin, address_space=address_space]) -> UnsafePointerV2[type, MutableAnyOrigin, address_space=address_space]
Casts the origin of a mutable pointer to MutableAnyOrigin.
This requires the pointer to already be mutable as casting mutability is inherently very unsafe.
It is usually preferred to maintain concrete origin values instead of
using MutableAnyOrigin. However, if it is needed, keep in mind that
MutableAnyOrigin can alias any memory value, so Mojo's ASAP
destruction will not apply during the lifetime of the pointer.
Returns:
UnsafePointerV2: A pointer with the origin set to MutableAnyOrigin.
address_space_cast
address_space_cast[target_address_space: AddressSpace = address_space](self) -> UnsafePointerV2[type, origin, address_space=target_address_space]
Casts an UnsafePointer to a different address space.
Parameters:
- target_address_space (
AddressSpace): The address space of the result.
Returns:
UnsafePointerV2: A new UnsafePointer object with the same type and the same address,
as the original UnsafePointer and the new address space.
destroy_pointee
destroy_pointee(self: UnsafePointerV2[type, origin])
Destroy the pointed-to value.
The pointer must not be null, and the pointer memory location is assumed
to contain a valid initialized instance of type. This is equivalent to
_ = self.take_pointee() but doesn't require Movable and is
more efficient because it doesn't invoke __moveinit__.
take_pointee
take_pointee[T: Movable, //](self: UnsafePointerV2[T, origin]) -> T
Move the value at the pointer out, leaving it uninitialized.
The pointer must not be null, and the pointer memory location is assumed
to contain a valid initialized instance of T.
This performs a consuming move, ending the origin of the value stored
in this pointer memory location. Subsequent reads of this pointer are
not valid. If a new valid value is stored using init_pointee_move(), then
reading from this pointer becomes valid again.
Parameters:
- T (
Movable): The type the pointer points to, which must beMovable.
Returns:
T: The value at the pointer.
init_pointee_move
init_pointee_move[T: Movable, //](self: UnsafePointerV2[T, origin], var value: T)
Emplace a new value into the pointer location, moving from value.
The pointer memory location is assumed to contain uninitialized data,
and consequently the current contents of this pointer are not destructed
before writing value. Similarly, ownership of value is logically
transferred into the pointer location.
When compared to init_pointee_copy, this avoids an extra copy on
the caller side when the value is an owned rvalue.
Parameters:
- T (
Movable): The type the pointer points to, which must beMovable.
Args:
- value (
T): The value to emplace.
init_pointee_copy
init_pointee_copy[T: Copyable, //](self: UnsafePointerV2[T, origin], value: T)
Emplace a copy of value into the pointer location.
The pointer memory location is assumed to contain uninitialized data,
and consequently the current contents of this pointer are not destructed
before writing value. Similarly, ownership of value is logically
transferred into the pointer location.
When compared to init_pointee_move, this avoids an extra move on
the callee side when the value must be copied.
Parameters:
- T (
Copyable): The type the pointer points to, which must beCopyable.
Args:
- value (
T): The value to emplace.
init_pointee_move_from
init_pointee_move_from[T: Movable, //](self: UnsafePointerV2[T, origin], src: UnsafePointerV2[T, origin])
Moves the value src points to into the memory location pointed to by self.
The self pointer memory location is assumed to contain uninitialized
data prior to this assignment, and consequently the current contents of
this pointer are not destructed before writing the value from the src
pointer.
Ownership of the value is logically transferred from src into self's
pointer location.
After this call, the src pointee value should be treated as
uninitialized data. Subsequent reads of or destructor calls on the src
pointee value are invalid, unless and until a new valid value has been
moved into the src pointer's memory location using an
init_pointee_*() operation.
This transfers the value out of src and into self using at most one
__moveinit__() call.
Example
var a_ptr = UnsafePointer.alloc[String](1)
var b_ptr = UnsafePointer.alloc[String](2)
# Initialize A pointee
a_ptr.init_pointee_move("foo")
# Perform the move
b_ptr.init_pointee_move_from(a_ptr)
# Clean up
b_ptr.destroy_pointee()
a_ptr.free()
b_ptr.free()Safety
selfandsrcmust be non-nullsrcmust contain a valid, initialized instance ofT- The pointee contents of
selfshould be uninitialized. Ifselfwas previously written with a valid value, that value will be be overwritten and its destructor will NOT be run.
Parameters:
- T (
Movable): The type the pointer points to, which must beMovable.
Args:
- src (
UnsafePointerV2): Source pointer that the value will be moved from.
Was this page helpful?
Thank you! We'll create more content like this.
Thank you for helping us improve!