Conceptual
Login

Wrapping a C Structure with Opaque

Rust wants to know the exact contents and invariants of every value it holds, but a C kernel structure is something only C may interpret, may be modified behind your back, and often must not move. Opaque is the wrapper that says: here is a piece of memory of the right size and alignment holding a C structure, treat it as a black box, and here is the raw pointer to hand back to C. Nearly every Rust type that represents a kernel object is an Opaque with safe methods bolted onto it. Without such a wrapper, Rust would assume it may copy or reorder a structure that C is still pointing at.

Questions this Concept answers

  • Why must a C structure that the kernel still points at be held in a wrapper rather than as an ordinary Rust value?