#pointers #convenience #wrapper #minimal #no-std #no-alloc

no-std maybe_null

A pointer type for handling potentially null values without accidental dereference

4 releases

0.2.0 Dec 4, 2024
0.1.2 Dec 4, 2024
0.1.1 Dec 4, 2024
0.1.0 Dec 4, 2024

#1192 in Rust patterns

Download history 308/week @ 2024-12-02

308 downloads per month

Custom license

14KB
281 lines

MaybeNull

The MaybeNull type is used for hinting that a pointer may be null, and still prevent accidental null pointer dereference.

use maybe_null::MaybeNull;

let ptr = MaybeNull::<u32>::null();

let Some(ptr) = ptr.get() {
  // We know the pointer is non-null here.

  // Whoops! this will never happen because our `ptr` was initialized as null!
  ptr.write(0);
}

AtomicMaybeNull

The AtomicMaybeNull works like MaybeNull but allows for atomic access of the pointer.

No runtime deps