Rust Unsafe Boundaries
Use this skill to isolate unsafe Rust behind small, documented, testable
boundaries. Unsafe code is acceptable only when a safe API cannot express the
needed operation with acceptable correctness and performance.
Core Workflow
- Try the safe design first. Check standard APIs, ownership restructuring,
iterators, synchronization primitives, and existing crates.
- State the invariant that safe Rust cannot prove. If the invariant cannot be
written down, do not write unsafe code yet.
- Keep unsafe blocks tiny. Put runtime checks and setup in safe code before the
block.
- Add a
// SAFETY: comment immediately before each unsafe block explaining
why every unsafe operation inside is valid.
- Mark a function
unsafe fn only when callers must uphold extra conditions.
Document those conditions in a # Safety section.
- Enable or respect
unsafe_op_in_unsafe_fn; unsafe operations inside unsafe
functions should still be wrapped in explicit unsafe blocks.
- Test normal behavior, boundary cases, panic paths, and drop behavior. Run
Miri when the project supports it.
Boundary Rules
Read references/safety-invariants.md before adding or approving unsafe code.
- Prefer private unsafe internals plus a safe public wrapper.
- Prefer
MaybeUninit<T> over deprecated or ad hoc uninitialized memory
patterns.
- Never create references from raw pointers unless validity, alignment,
initialization, aliasing, and lifetime are all proven.
- Do not use
set_len, pointer arithmetic, or from_raw_parts without proving
capacity, initialization, and ownership.
- Make panic safety explicit when partially initialized values, manual drops, or
length changes are involved.
- Avoid
static mut; prefer OnceLock, LazyLock, atomics, or locked state.
Documentation Pattern
/// # Safety
///
/// `ptr` must be non-null, aligned for `T`, initialized, and valid for reads
/// for the returned lifetime. No mutable reference may alias the same value.
pub unsafe fn read_ref<'a, T>(ptr: *const T) -> &'a T {
// SAFETY: The caller guarantees `ptr` satisfies the documented contract.
unsafe { &*ptr }
}
Review Checklist
- Every unsafe block has a local
SAFETY explanation.
- Every
unsafe fn or unsafe trait has a # Safety contract.
- Public safe APIs cannot be used to violate internal invariants.
- Drop, panic, and early-return paths preserve initialization and ownership.
- Tests or Miri cover the dangerous edge, not only the happy path.
Source: hashgraph-online/awesome-codex-plugins → plugins/LVTD-LLC/skills/skills/rust-unsafe-boundaries/SKILL.md
1---2name: rust-unsafe-boundaries3description: Isolate and review unsafe Rust behind small, documented, testable boundaries with explicit invariants. Use when writing, refactoring, or reviewing unsafe code, raw pointers, unsafe functions, unsafe traits, MaybeUninit, pointer aliasing, panic safety, Miri checks, or safe abstractions over unsafe internals.4---567# Rust Unsafe Boundaries89Use this skill to isolate unsafe Rust behind small, documented, testable10boundaries. Unsafe code is acceptable only when a safe API cannot express the11needed operation with acceptable correctness and performance.1213## Core Workflow14151. Try the safe design first. Check standard APIs, ownership restructuring,16 iterators, synchronization primitives, and existing crates.172. State the invariant that safe Rust cannot prove. If the invariant cannot be18 written down, do not write unsafe code yet.193. Keep unsafe blocks tiny. Put runtime checks and setup in safe code before the20 block.214. Add a `// SAFETY:` comment immediately before each unsafe block explaining22 why every unsafe operation inside is valid.235. Mark a function `unsafe fn` only when callers must uphold extra conditions.24 Document those conditions in a `# Safety` section.256. Enable or respect `unsafe_op_in_unsafe_fn`; unsafe operations inside unsafe26 functions should still be wrapped in explicit unsafe blocks.277. Test normal behavior, boundary cases, panic paths, and drop behavior. Run28 Miri when the project supports it.2930## Boundary Rules3132Read `references/safety-invariants.md` before adding or approving unsafe code.3334- Prefer private unsafe internals plus a safe public wrapper.35- Prefer `MaybeUninit<T>` over deprecated or ad hoc uninitialized memory36 patterns.37- Never create references from raw pointers unless validity, alignment,38 initialization, aliasing, and lifetime are all proven.39- Do not use `set_len`, pointer arithmetic, or `from_raw_parts` without proving40 capacity, initialization, and ownership.41- Make panic safety explicit when partially initialized values, manual drops, or42 length changes are involved.43- Avoid `static mut`; prefer `OnceLock`, `LazyLock`, atomics, or locked state.4445## Documentation Pattern4647```rust48/// # Safety49///50/// `ptr` must be non-null, aligned for `T`, initialized, and valid for reads51/// for the returned lifetime. No mutable reference may alias the same value.52pub unsafe fn read_ref<'a, T>(ptr: *const T) -> &'a T {53 // SAFETY: The caller guarantees `ptr` satisfies the documented contract.54 unsafe { &*ptr }55}56```5758## Review Checklist5960- Every unsafe block has a local `SAFETY` explanation.61- Every `unsafe fn` or unsafe trait has a `# Safety` contract.62- Public safe APIs cannot be used to violate internal invariants.63- Drop, panic, and early-return paths preserve initialization and ownership.64- Tests or Miri cover the dangerous edge, not only the happy path.6566---6768**Source:** [`hashgraph-online/awesome-codex-plugins`](https://github.com/hashgraph-online/awesome-codex-plugins) → `plugins/LVTD-LLC/skills/skills/rust-unsafe-boundaries/SKILL.md`