Reinventing From Scratch — Box<T>
Chapter 8 — NonNull, ZSTs, and Niche Optimization
8.1 NonNull<T>
A wrapper guaranteeing a pointer is never null. This enables niche optimization: Option<NonNull<T>> can use the null bit-pattern to represent None without extra space.
use std::ptr::NonNull;
pub struct MyBoxNN<T> {
ptr: NonNull<T>,
}
⚠️ Invariant: Never construct a
NonNullfrom a null pointer. Allocate first, then wrap.
8.2 ZSTs (Zero-Sized Types)
- ZSTs like
()have size 0, butBox<()>still carries ownership semantics. - The standard
Boxensures its internal pointer is non-null, keeping niche optimizations available (e.g.,Option<Box<T>>same size as a pointer).
8.3 When to switch to NonNull
- When you want to formalize never-null internally.
- When optimizing enum sizes matters.
Exercise
Show that std::mem::size_of::<Option<Box::<u8>>>() == std::mem::size_of::<*const u8>() (on typical platforms). Explain why.
Deep Dive: Ownership Proofs, Drop Order, and DST Considerations
A. Formal Invariants for MyBox<T> (Sized)
- B1 (Pointer Validity):
ptris either null only afterinto_rawor a valid, properly aligned pointer to initializedT. - B2 (Single Drop): The destructor of
Tis invoked exactly once if and only ifptris non-null atDroptime. - B3 (Dealloc after Drop):
dealloc(layout_of::<T>())is called exactly once, and only afterdrop_in_place. - B4 (From/Into Raw Consistency):
from_rawonly accepts pointers produced byinto_rawof the same type/allocator; mixing allocators is UB. - B5 (No References to Uninit): No
&/&mutreferences are created beforeptr::writeinitializes the allocation.
B. Proof Sketches
B.1 Single Drop — Drop checks for null and calls drop_in_place once; into_raw nulls out ptr and forgets self, preventing Drop from running on a live value.
B.2 No Use-After-Free — Deallocation happens only after the destructor; references returned by Deref are derived from a live ptr and never stored beyond the box’s lifetime.
B.3 Panic Safety — If constructor panics before publishing, no ownership is established; if Drop panics (should not), process aborts, avoiding double-unwind corruption.
C. DST Box Notes
- Slices (
Box<[T]>): store length; the fat pointer (data, len) enables correct deallocation. Box<str>: same as[u8]with UTF‑8 invariant; length in metadata.Box<dyn Trait>: fat pointer (data, vtable); the vtable encodes drop and size/alignment; std uses compiler magic for correct layout.
D. Interop Patterns
- FFI Ownership Transfer:
into_raw-> C takes ownership; C must call back into Rust withfrom_rawor a custom free. - Leaking Globals:
leakreturns'staticreference, acceptable for process lifetime singletons; document intent.
E. Debugging
- Double Drop: look for
*passignment instead ofptr::writeon uninitialized memory. - Mismatched Layout: using
deallocwith wrongLayoutcauses heap corruption; keepLayout::new::<T>()paired.
F. Exercises
- Implement
try_newreturningResult<MyBox<T>, AllocError>. - Add
into_inner(self) -> Tbyptr::readand skipping dealloc? Explain why you must still dealloc after movingT. - Implement
MyVec::into_boxed_slicethat handsRawVecbuffer to aBox<[T]>safely.
FAQ (Extended)
Q: Does Box<T> guarantee a stable address? A: Yes, the pointee’s address is stable for the life of the box; moving the box moves only the handle.
Q: Why ptr::write not *p = value? A: The latter reads/drops the previous contents (uninitialized), which is UB.
Q: Can Box<T> be null? A: By design, standard Box<T> is non-null; our MyBox may set ptr = null only as a consumed sentinel post-into_raw.
Q: Is Pin<Box<T>> needed for stable address? A: Not for stability; Pin is for forbidding moves via the API.