Mojo struct
UnsafeMaybeUninitialized
A memory location that may or may not be initialized.
Note that the destructor is a no-op. If the memory was initialized, the caller
is responsible for calling assume_initialized_destroy
before the memory is
deallocated.
Every method in this struct is unsafe and the caller must know at all times if the memory is initialized or not. Calling a method that assumes the memory is initialized when it is not will result in undefined behavior.
Parametersβ
- βElementType (
AnyType
): The type of the element to store.
Aliasesβ
type = array<1, !kgen.paramref<:trait<@stdlib::@builtin::@anytype::@AnyType> ElementType>>
:
Implemented traitsβ
AnyType
,
CollectionElementNew
,
ExplicitlyCopyable
,
Movable
Methodsβ
__init__
β
__init__(inout self: Self)
The memory is now considered uninitialized.
__init__[MovableType: Movable](inout self: UnsafeMaybeUninitialized[MovableType], owned value: MovableType)
The memory is now considered initialized.
Parameters:
- βMovableType (
Movable
): The type of the element to store.
Args:
- βvalue (
MovableType
): The value to initialize the memory with.
__copyinit__
β
__copyinit__(inout self: Self, other: Self)
Copy another object.
This method should never be called as implicit copy should not be done on memory that may be uninitialized.
Trying to call this method will abort.
If you wish to perform a copy, you should manually call the method
copy_from
instead.
Args:
- βother (
Self
): The object to copy.
__moveinit__
β
__moveinit__(inout self: Self, owned other: Self)
Move another object.
This method should never be called as implicit moves should not be done on memory that may be uninitialized.
Trying to call this method will abort.
If you wish to perform a move, you should manually call the method
move_from
instead.
Args:
- βother (
Self
): The object to move.
__del__
β
__del__(owned self: Self)
This is a no-op.
Calling this method assumes that the memory is uninitialized.
If the memory was initialized, the caller should
use assume_initialized_destroy
before.
copy_from
β
copy_from[CopyableType: ExplicitlyCopyable](inout self: UnsafeMaybeUninitialized[CopyableType], other: UnsafeMaybeUninitialized[CopyableType])
Copy another object.
This function assumes that the current memory is uninitialized and the other object is initialized memory.
Parameters:
- βCopyableType (
ExplicitlyCopyable
): The type object to copy.
Args:
- βother (
UnsafeMaybeUninitialized[CopyableType]
): The object to copy.
copy_from[CopyableType: ExplicitlyCopyable](inout self: UnsafeMaybeUninitialized[CopyableType], other: CopyableType)
Copy another object.
This function assumes that the current memory is uninitialized.
Parameters:
- βCopyableType (
ExplicitlyCopyable
): The type object to copy.
Args:
- βother (
CopyableType
): The object to copy.
move_from
β
move_from[MovableType: Movable](inout self: UnsafeMaybeUninitialized[MovableType], inout other: UnsafeMaybeUninitialized[MovableType])
Move another object.
This function assumes that the current memory is uninitialized and the other object is initialized memory.
After the function is called, the other object is considered uninitialized.
Parameters:
- βMovableType (
Movable
): The type object to move.
Args:
- βother (
UnsafeMaybeUninitialized[MovableType]
): The object to move.
move_from[MovableType: Movable](inout self: UnsafeMaybeUninitialized[MovableType], other: UnsafePointer[MovableType, 0, 0, alignof[::AnyType,__mlir_type.!kgen.target]() if triple_is_nvidia_cuda() else 1])
Move another object.
This function assumes that the current memory is uninitialized and the other object is initialized memory.
After the function is called, the other
object is considered uninitialized.
Parameters:
- βMovableType (
Movable
): The type object to move.
Args:
- βother (
UnsafePointer[MovableType, 0, 0, alignof[::AnyType,__mlir_type.!kgen.target]() if triple_is_nvidia_cuda() else 1]
): The pointer to the object to move.
write
β
write[MovableType: Movable](inout self: UnsafeMaybeUninitialized[MovableType], owned value: MovableType)
Write a value into an uninitialized memory location.
Calling this method assumes that the memory is uninitialized.
Parameters:
- βMovableType (
Movable
): The type of the element to store.
Args:
- βvalue (
MovableType
): The value to write.
assume_initialized
β
assume_initialized(ref [self_is_lifetime] self: Self) -> ref [_] ElementType
Returns a reference to the internal value.
Calling this method assumes that the memory is initialized.
Returns:
A reference to the internal value.
unsafe_ptr
β
unsafe_ptr(self: Self) -> UnsafePointer[ElementType, 0, 0, alignof[::AnyType,__mlir_type.!kgen.target]() if triple_is_nvidia_cuda() else 1]
Get a pointer to the underlying element.
Note that this method does not assumes that the memory is initialized or not. It can always be called.
Returns:
A pointer to the underlying element.
assume_initialized_destroy
β
assume_initialized_destroy(inout self: Self)
Runs the destructor of the internal value.
Calling this method assumes that the memory is initialized.
Was this page helpful?
Thank you! We'll create more content like this.
Thank you for helping us improve!
If you'd like to share more information, please report an issue on GitHub
π What went wrong?