rust-linear-type_skill

This skill analyzes and applies linear type principles to Rust ownership, ensuring resource safety, unique ownership, and deterministic destruction.
  • Shell

20

GitHub Stars

3

Bundled Files

2 months ago

Catalog Refreshed

4 months ago

First Indexed

Readme & install

Copy the install command, review bundled files from the catalogue, and read any extended description pulled from the listing source.

Installation

Preview and clipboard use veilstrat where the catalogue uses aiagentskills.

npx veilstrat add skill huiali/rust-skills --skill rust-linear-type

  • SKILL_EN.md1.4 KB
  • SKILL_ZH.md5.1 KB
  • SKILL.md5.1 KB

Overview

This skill is a Rust linear-type and resource-management expert focused on designing and verifying linear semantics, destructible resources, and exclusive ownership patterns. It provides concrete patterns (Destructible, unique pointers, resource tokens, transactional resources) and guidance to ensure resources are released exactly once. The goal is practical recommendations and code-level strategies to implement linear behavior on top of Rust’s ownership model.

How this skill works

The skill inspects code and design to detect non-linear patterns (unwanted Clone/Rc, manual frees, missing Drop guarantees) and recommends linear alternatives: private constructors, consume-by-value APIs, ManuallyDrop guards, UniquePtr semantics, and transaction-style commit/rollback. It evaluates resource lifecycle points (creation, transfer, consumption, Drop) and suggests refactors to enforce single-consume semantics. It also proposes test cases and safety checks for FFI and unsafe code paths.

When to use it

  • Implementing RAII for OS handles, sockets, or allocated memory that must be freed exactly once.
  • Designing FFI wrappers where external resources require precise release semantics.
  • Building APIs that must prevent duplication or shared ownership (e.g., exclusive file handles).
  • Creating transactional resource flows that commit or rollback deterministically.
  • Hardening unsafe code paths to avoid double-free, use-after-free, or leaked resources.

Best practices

  • Prefer move/consume APIs: take self by value and return resources via consume() to guarantee single use.
  • Avoid deriving Clone/Copy for resource-owning types; use private constructors to control creation.
  • Wrap raw allocations in Drop or Destructible implementations and use ManuallyDrop to prevent double-free when needed.
  • Model temporary ownership with ResourceToken or Transaction types to express consumed vs. unconsumed states.
  • Prefer RAII + Drop for common resources and reserve unsafe patterns for minimal, reviewed hotspots.
  • Provide tests for lifecycle: ensure close/free called exactly once and that rollback paths drop safely.

Example use cases

  • Wrap a platform file descriptor in a LinearFile type that can be moved and consumed but never cloned.
  • Implement a UniquePtr for a C-allocated buffer returned by FFI and ensure it is freed exactly once in Drop.
  • Create a Transaction wrapper around in-memory state or external resources to support commit/rollback semantics.
  • Use ResourceToken to model a one-time consumable API for streaming or single-use cryptographic keys.
  • Audit a codebase for Rc/Arc misuse on handles and replace with linear tokens or exclusive types where appropriate.

FAQ

Rust’s ownership and move semantics provide much of the ground work, but Rust is not strictly linear: Clone/Copy and shared pointers permit non-linear use. This skill shows patterns to enforce one-time consumption and prevent accidental duplication.

When should I use ManuallyDrop?

Use ManuallyDrop when you need explicit control over destructor timing (e.g., to avoid double-free during a controlled move or when composing destructors), and ensure unsafe blocks are minimal and reviewed.

Built by
VeilStrat
AI signals for GTM teams
© 2026 VeilStrat. All rights reserved.All systems operational
rust-linear-type skill by huiali/rust-skills | VeilStrat