Class Subspace :: sus :: ptr :: NonNull

template <class T>
class NonNull
requires
!std::is_reference_v<T>
{ ... };

A pointer wrapper which holds a never-null pointer.

A NonNull can not be implicitly created from an array, as that would throw away the length information. Explicitly cast to a pointer to use NonNull with an array.

The NonNull type is trivially copyable and moveable.

NonNull satisifes the NeverValueField, so Option<NonNull<T>> has the same size as NonNull, similar to Option<T&>.

TODO: Make a NonNullArray type? https://godbolt.org/z/3vW3xsz5h

Static Methods

Constructs a NonNull<T> from a reference to T.

static auto from(T& t) -> NonNull<T>
template <class U, size_t N>
static auto from(U (&t)[N]) -> NonNull<T>
deleted

Satisfies the sus::construct::From<NonNull<T>, T&> concept.

template <class U>
static auto with_ptr(U t) -> Option<NonNull<T>>
requires
sus::ptr::SameOrSubclassOf<U, T *>
template <class U, size_t N>
static auto with_ptr(U (&t)[N]) -> Option<NonNull<T>>
deleted

Constructs a NonNull<T> from a pointer to T.

Does not implicitly convert from an array. The caller must explicitly convert it to a pointer to throw away the length of the array.

Panics

The method will panic if the pointer t is null.

template <class U>
static auto with_ptr_unchecked(UnsafeFnMarker, U t) -> NonNull<T>
requires
sus::ptr::SameOrSubclassOf<U, T *>
template <class U, size_t N>
static auto with_ptr_unchecked(U (&t)[N]) -> NonNull<T>
deleted

Constructs a NonNull<T> from a pointer to T.

Does not implicitly convert from an array. The caller must explicitly convert it to a pointer to throw away the length of the array.

Safety

This method must not be called with a null pointer, or Undefined Behaviour results.

Methods

auto as_mut() -> T&
requires
!std::is_const_v<T>

Returns a mutable reference to the pointee.

This method is only callable when the pointer is not const.

auto as_mut_ptr() -> T*
requires
!std::is_const_v<T>

Returns a mutable pointer to the pointee.

This method is only callable when the pointer is not const.

auto as_ptr() const -> const T*

Returns a const pointer to the pointee.

auto as_ref() const -> const T&

Returns a const reference to the pointee.

template <class U>
auto cast() const -> NonNull<U>
requires
sus::ptr::SameOrSubclassOf<T *, U *>

Cast the pointer of type T in NonNull<T> to a pointer of type U and return a NonNull<U>.

This requires that T* is a subclass of U*. To perform a downcast, like static_cast<U*> allows, use downcast().

template <class U>
auto downcast(UnsafeFnMarker) const -> NonNull<U>

Cast the pointer of type T in NonNull<T> to a pointer of type U and return a NonNull<U>.

Safety

The pointee must be a U* or this results in Undefined Behaviour.

Operators

auto operator->() const -> const T*
auto operator->() -> T*
requires
!std::is_const_v<T>

Gives access to the object pointed to by NonNull.

Mutable access is only given is NonNull is not const and the pointer within is not const.