Skip to main content
Log in

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?