Mojo struct
OwnedPointer
struct OwnedPointer[T: AnyType]
A safe, owning, smart pointer.
This smart pointer is designed for cases where there is clear ownership of the underlying data, and restricts access to it through the origin system such that no more than one mutable alias for the underlying data may exist.
For a comparison with other pointer types, see Intro to pointers in the Mojo Manual.
Parameters
- T (
AnyType
): The type to be stored in theOwnedPointer
.
Implemented traits
AnyType
,
UnknownDestructibility
Methods
__init__
__init__[T: Movable](out self: OwnedPointer[T], owned value: T)
Construct a new OwnedPointer
by moving the passed value into a new backing allocation.
Parameters:
- T (
Movable
): The type of the data to store. It is restricted toMovable
here to allow efficient move construction.
Args:
- value (
T
): The value to move into theOwnedPointer
.
__init__[T: ExplicitlyCopyable](out self: OwnedPointer[T], *, copy_value: T)
Construct a new OwnedPointer
by explicitly copying the passed value into a new backing allocation.
Parameters:
- T (
ExplicitlyCopyable
): The type of the data to store, which must beExplicitlyCopyable
.
Args:
- copy_value (
T
): The value to explicitly copy into theOwnedPointer
.
__init__[T: Copyable, U: NoneType = None](out self: OwnedPointer[T], value: T)
Construct a new OwnedPointer
by copying the passed value into a new backing allocation.
Parameters:
- T (
Copyable
): The type of the data to store. - U (
NoneType
): A dummy type parameter, to lower the selection priority of this ctor.
Args:
- value (
T
): The value to copy into theOwnedPointer
.
__init__[T: ExplicitlyCopyable](out self: OwnedPointer[T], *, other: OwnedPointer[T])
Construct a new OwnedPointer
by explicitly copying the value from another OwnedPointer
.
Parameters:
- T (
ExplicitlyCopyable
): The type of the data to store.
Args:
- other (
OwnedPointer[T]
): TheOwnedPointer
to copy.
__moveinit__
__moveinit__(out self, owned existing: Self)
Move this OwnedPointer
.
Args:
- existing (
Self
): The value to move.
__del__
__del__(owned self)
Destroy the OwnedPointer[].
__getitem__
__getitem__(ref self) -> ref [self] T
Returns a reference to the pointers's underlying data with parametric mutability.
Returns:
A reference to the data underlying the OwnedPointer
.
unsafe_ptr
unsafe_ptr(self) -> UnsafePointer[T]
UNSAFE: returns the backing pointer for this OwnedPointer
.
Returns:
An UnsafePointer to the backing allocation for this OwnedPointer
.
take
take[T: Movable](owned self: OwnedPointer[T]) -> T
Move the value within the OwnedPointer
out of it, consuming the OwnedPointer
in the process.
Parameters:
- T (
Movable
): The type of the data backing thisOwnedPointer
.take()
only exists forT: Movable
since this consuming operation only makes sense for types that you want to avoid copying. For types that areCopyable
orExplicitlyCopyable
but are notMovable
, you can copy them through__getitem__
as invar v = some_ptr_var[]
.
Returns:
The data that is (was) backing the OwnedPointer
.
steal_data
steal_data(owned self) -> UnsafePointer[T]
Take ownership over the heap allocated pointer backing this OwnedPointer
.
Safety: This function is not unsafe to call, as a memory leak is not considered unsafe.
However, to avoid a memory leak, callers should ensure that the returned pointer is eventually deinitialized and deallocated. Failure to do so will leak memory.
Returns:
The pointer owned by this instance.
Was this page helpful?
Thank you! We'll create more content like this.
Thank you for helping us improve!