Skip to content
CryoCryo home
Stdlibcore

ptr

import std::core::ptr; · source

NonNull<T>

type struct NonNull<T> {
    ptr: T*;

    static new(raw: T*) -> NonNull<T>;
    as_ptr(&this) -> T*;
    offset(&this, count: i64) -> NonNull<T>;
    cast<U>(&this) -> NonNull<U>;
}

A raw pointer that is guaranteed non-null by construction. Check once at the ownership boundary and let everything downstream rely on the invariant. The owning smart pointers — Box, Rc, Arc — build on this and live in alloc.

import std::core::ptr;

mut p: NonNull<i32> = NonNull<i32>::new(raw);   // aborts if `raw` is null
const q: NonNull<i32> = p.offset(3);            // 3 elements, not 3 bytes

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