Skip to content
CryoCryo home
Stdlibcore

pointer

import std::core::pointer; · source

Types

NonNull<T>

type struct NonNull<T> {
    private ptr: mut T*;
}

A raw, writable pointer that is guaranteed non-null by construction. Check once at the ownership boundary and let everything downstream rely on the invariant. A NonNull does not own its pointee and releases nothing; the owning smart pointers — Box, Rc, Arc — live in alloc.

import std::core::pointer::{ NonNull };

mut values: [i32] = [1, 2, 3, 4, 5];
const raw: mut i32* = &values[0];
const p: NonNull<i32> = NonNull::<i32>::new(raw);   // aborts if `raw` is null
const q: NonNull<i32> = p.offset(3);              // 3 elements, not 3 bytes
const back: mut i32* = q.as_pointer();

cast<U> cannot check that the alignment requirements line up; that is on you.

Constructors

static new(raw: mut T*) -> NonNull<T>;

Construct from a raw pointer, aborting if it's null. Check once at the ownership boundary so downstream code can rely on the invariant.

Methods

as_pointer(&this) -> mut T*;

The pointer, never null.

offset(&this, count: i64) -> NonNull<T>;

Offset by count elements (not bytes). Negative values walk backwards.

cast<U>(&this) -> NonNull<U>;

Reinterpret this pointer as pointing to a different type. The new type must share alignment requirements with T; the compiler cannot check this.

Intrinsics

intrinsic function read<T>(source: T*) -> T;

Takes the value source points to out of its storage, by its bits. The storage still holds those bits and must not release them again: the program writes a new value over it (write, or a store through a raw pointer), frees it without releasing it, or lets it fall past a collection's length. A store through a reference that source was taken from behind releases nothing afterwards. This is the take that *source in a by-value position is refused as (E0453).

intrinsic function write<T>(target: mut T*, value: T) -> void;

Stores value into the storage target points to, releasing nothing the storage held: what was there was taken out with read, or was never written. A live value overwritten this way is never released.

intrinsic function drop_in_place<T>(p: T*) -> void;

Releases the value p points to in place, the way a value leaving scope is released: its type's Drop implementation and then its fields' glue, the glue alone for a type with no implementation, nothing for a Copy value. T takes no bound, so generic code can release a value of any type through it, where calling drop on an unbounded T names no method. The memory itself is not freed, and the value must not be used or released again.