Mojo struct
UnsafeMaybeUninitialized
struct UnsafeMaybeUninitialized[ElementType: AnyType]
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, :trait<@stdlib::@builtin::@anytype::@AnyType> ElementType>
:
Implemented traits
AnyType
,
CollectionElement
,
CollectionElementNew
,
Copyable
,
ExplicitlyCopyable
,
Movable
,
UnknownDestructibility
Methods
__init__
__init__(out self)
The memory is now considered uninitialized.
__init__[MovableType: Movable](out 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__(out 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__(out 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)
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](mut 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](mut 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](mut self: UnsafeMaybeUninitialized[MovableType], mut 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](mut self: UnsafeMaybeUninitialized[MovableType], other: UnsafePointer[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 (
UnsafePointer[MovableType]
): The pointer to the object to move.
write
write[MovableType: Movable](mut 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) -> ref [self] 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) -> UnsafePointer[ElementType]
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(mut 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!