Start with the idea
An unsafe trait means incorrect implementation can cause undefined behavior in code that trusts it. Document every invariant precisely and keep the unsafe impl beside evidence that it holds.
An unsafe trait is a promise whose incorrect implementation can invalidate assumptions made by other code. Its documentation must state the invariant precisely: which operations, lifetimes, alignment, or thread interactions are safe, and what every implementor must maintain. The unsafe keyword on the impl means the implementor has checked that contract; it is not a compiler proof. A generic caller may rely on the trait bound and therefore cannot recheck each concrete implementation.
Avoid vague contracts such as “instances never expose moves” unless the type truly enforces that property. A plain FixedBuffer can be moved between bindings, so claiming stable address for it would be unsound.
Keep learning, for free
The rest of this lesson is waiting for you.
Create a free account to read the complete explanation, see the infographic, explore the code, and try the exercises. All 742 lessons and algorithm steps are included.
No payment details. Your account opens the complete Academy.