rust-unsafe-boundaries
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.
- License 许可
- MIT
- 最近更新
- 2026-11-14
- Stars
- ★ 1.6k
- Forks
- 491
01 前言 Front Matter
- name
- rust-unsafe-boundaries
- license
- MIT
- metadata
- { "tags": "rust,unsafe,raw-pointers,memory-safety,miri", "version": "0.1.0", "category": "Rust", "displayName": "Rust Unsafe Boundaries" }
- description
- 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.
- compatibility
- Codex, Claude Code, and other Agent Skills-compatible clients.
02 资源文件 2
- 参考文档agents/openai.yaml233 B
- 参考文档references/safety-invariants.md1.3 KB
03 SKILL.md 正文
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 fnonly when callers must uphold extra conditions. Document those conditions in a# Safetysection. - 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, orfrom_raw_partswithout proving capacity, initialization, and ownership. - Make panic safety explicit when partially initialized values, manual drops, or length changes are involved.
- Avoid
static mut; preferOnceLock,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
SAFETYexplanation. - Every
unsafe fnor unsafe trait has a# Safetycontract. - 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.
04 同仓库更多 Skills
worktable-review-with-annotations
Create, inspect, reply to, update, or resolve situated Worktable annotations. Use when feedback belongs to a Doc section, block, text range, or HTML Doc, or when the user asks to address existing review feedback and coordinate a requested artifact edit.
worktable-find-and-synthesize-context
Find, assess, and synthesize context from Worktable Docs and Records. Use when the user asks what a workspace contains, wants workspace-backed research, needs current facts reconciled across artifacts, or requests a cited synthesis without changing workspace state.
worktable-create-or-update-docs
Create, edit, rename, or maintain narrative Worktable Docs. Use for plans, decisions, briefs, notes, research syntheses, and other durable prose when the user requests a new artifact or a safe update to an existing one.
worktable-create-or-manage-records
Organize repeated structured information as durable Worktable Records. Use for research datasets, inventories, directories, pipelines, trackers, and other collections whose items need identity, fields, filtering, links, validation, or individual updates.
worktable-create-or-manage-interactive-html
Create or improve durable visual and interactive artifacts in Worktable. Use for dashboards, trackers, calculators, mini apps, slide presentations, and other requests that benefit from a polished HTML Doc.
worktable-collaborate-threads
Read, answer, and hand off messages through durable Worktable threads with users or other authenticated participants. Use when the user asks to check addressed messages, reply to a participant, continue a thread, request another agent's help, wait briefly for a response, or watch for comments and messages sent to the agent.