Skip to content
nim-ownership-hooks logo

Nim Ownership Hooks

nim-ownership-hooks

Implement and review Nim ARC/ORC ownership hooks for types that manually manage resources, including move-only, deep-copy, refcounted, and copy-on-write patterns. Use when a Nim type owns pointers, buffers, file descriptors, or custom heap memory and you need correct `=destroy`, `=copy`, `=dup`, ...

SKILL.md

Full skill instructions

Nim Ownership Hooks

1. Preamble

Use this skill when writing or reviewing Nim ownership hooks under ARC or ORC.

Start by classifying the type's ownership model. Then implement exactly the hook set that model requires — no more, no less. Do not force one ownership model onto another: a shared handle should not become move-only just because it has a destructor, and a deep-owning container should not pretend copies are cheap shares.

Complete examples for each ownership model live in references/.

2. Rules

When to write hooks

Do not write hooks unless the type owns a resource the compiler cannot release on its own.

The compiler auto-manages destruction for primitives, string, seq[T], ref T, array, tuples, closures, and objects whose fields are all auto-managed. Hooks lift through nesting: if a field's type already has custom hooks, the enclosing type gets correct compiler-generated hooks for free.

Custom hooks are needed only for non-managed resources: raw pointers (ptr T) to manually allocated memory, OS file descriptors, socket handles, distinct types whose base has no hooks, or similar. distinct types whose base already has hooks lift them automatically.

Hook-by-hook rules

=destroy

  • Check the moved-from sentinel (nil or default handle) before touching fields. Release only resources the type still owns.
  • When the type owns raw storage containing elements with their own hooks, destroy each element before freeing the raw storage pointer.
  • =destroy is implicitly .raises: []. Keep destructor behavior non-raising.
  • Use the signature =destroy(x: T), not var T. Both compile, but T prevents accidental field mutation.

=wasMoved

  • Reset every field that =destroy checks, so that =destroy becomes a no-op on the same variable. For pointer fields, set to nil.
  • The compiler eliminates a =destroy call that follows =wasMoved on the same variable.

=sink

  • Do not write a custom =sink by default. The compiler synthesizes one from =destroy plus a raw move, and that is usually sufficient.
  • When a custom =sink is required: call =destroy(dest), then =wasMoved(dest), then transfer source fields. No self-assignment check — the compiler eliminates x = x before reaching your hook.
  • Do not use copyMem or whole-object raw moves — they bypass child hook semantics.

=copy

  • Deep-copy types: protect against self-assignment (if dest.data == src.data: return). Without it, x = x destroys the source before copying.
  • After the guard: =destroy(dest), =wasMoved(dest), then rebuild from source. Check that source data is non-nil before allocating.
  • For move-only types, use {.error.} (bare pragma, no custom message — the compiler ignores custom error strings).
  • For refcounted types: =copy does destroy-then-share. No pointer self-assignment guard needed — the counter increment balances the destroy.

=dup

  • Deep-owning containers: mark with {.nodestroy.} and build a fresh copy. Call =dup on each child element (not copyMem) so child hooks run.
  • Refcounted types: increment the counter and share the pointer. No {.nodestroy.} needed — the counter balances the implicit return-path destroy.

=trace

  • Only when all three conditions hold: the type manually owns storage, stored values can participate in ORC cycles, and ORC needs to traverse them.
  • Forward-declare =destroy and =trace alongside each other to prevent the mutual-use generation conflict.

lent T

  • Use lent T for immutable accessors that should borrow from a container instead of copying.

Declaration order

Declare all custom hooks before any proc, converter, iterator, closure, or generic instantiation that mentions the type. The compiler eagerly generates implicit hooks and will error if a custom hook appears afterward.

Safe order: type definition, then =destroy, =wasMoved, =copy, then =dup.

Only templates may safely appear between the type definition and the hooks. If a hook body needs a shared helper, write it as a template directly before the hooks.

importc procs are opaque and do not trigger implicit hook generation — they may appear before hooks.

Move semantics

  • move(x) forces move. Source is left in moved-from state. =wasMoved runs on the source.
  • ensureMove(x) is a compile-time annotation only. Works for rvalues and sink parameters. Fails compilation when applied to lvalues with destructors.
  • sink parameters are affine, not linear: the callee may consume the value once, or not at all.
  • Object and tuple fields are separate entities for sink last-use analysis.
  • When the compiler cannot prove a sink argument is last use, it inserts =copy or =dup before passing.

Edge cases

  • Zero-length allocations: Guard against alloc(0) in constructors and =copy. Only allocate when length is positive. alloc(0) may return nil or an invalid pointer.
  • Thread-aware allocation: When a refcounted payload may cross thread boundaries, switch between allocShared/deallocShared and alloc/dealloc using when compileOption("threads").

3. Workflow

Step 1: Classify the ownership model

Match the type to exactly one model. Use the hook set that model requires.

ModelHooks needed
Plain / auto-managedNone
Borrowing / viewNone. Use lent T for accessors.
Move-only owner=destroy, =wasMoved, =copy as {.error.}
Deep-owning container=destroy, =wasMoved, =copy, =dup
Shared / refcounted=destroy, =wasMoved, =dup, =copy

Step 2: Implement the minimal hook set

Follow the hook-by-hook rules in section 2. See references/ for complete examples of each model.

Step 3: Verify with --expandArc

nim c --expandArc:nameOfFunction yourfile.nim

Confirm the compiler inserts the hooks you expect. Check that synthesized hooks match intent.

Step 4: Run stress tests

Test these scenarios for every custom-hook type:

  • Move into another variable
  • Overwrite existing owned data
  • Copy independence (mutating copy does not affect original)
  • Dup independence
  • Destroy-after-move (destroy is a no-op)
  • Self-copy (x = x does not crash)
  • Sink from temporaries
  • Zero-length initialization (if the type has constructors)

4. Common Mistakes

MistakeWhy it is wrong
=destroy with var TBoth compile, but T prevents accidental field mutation inside the destructor.
Setting fields to nil inside =destroyUse =wasMoved for field reset. The compiler eliminates the subsequent destroy.
Declaring =dup before =copy=dup body can trigger implicit =copy generation, causing a conflict.
Missing self-assignment guard in deep-copy =copyDestroys source data before reading it.
Self-assignment check in =sinkCompiler already eliminates simple x = x. The check is dead code.
Missing {.nodestroy.} on deep-owning =dupCompiler destroys result before the caller receives it.
Custom =sink when synthesized is fineAdds unnecessary complexity with no benefit.
copyMem in =sink or =dupBypasses child hook semantics and breaks the ownership chain for elements that have their own hooks.
Missing zero-length guardalloc(0) may return nil; subsequent indexing crashes.
ensureMove on lvalue with destructorCompile-time error. Only valid for rvalues and sink params.
alloc in multi-threaded codeMust use allocShared/deallocShared instead.
Custom error string in {.error: "msg"} on =copyThe compiler ignores custom error messages. Use bare {.error.}.

5. References

  • references/​move_only_owner.md — exclusive resource ownership, no copy allowed
  • references/​deep_owning_container.md — manual allocation with deep copy
  • references/​shared_refcounted.md — refcounted handle (separate counter + generic SharedPtr)
  • references/​custom_sink.md — when and how to write a custom =sink

6. Changelog

  • 2026-04-07: Initial version with zero-length guards, non-var destroy, refcounted nuances.
  • 2026-04-08: Restructured. Examples moved to references/.
  • 2026-04-17: Removed redundant cow_string.md.

More Productivity & Planning skills

brainstorming logo
Productivity & Planning

brainstorming

Structured design dialogue that validates ideas before implementation begins.

295.4K 385.7K
View
ui-ux-pro-max logo
Productivity & Planning

ui-ux-pro-max

Comprehensive design intelligence for web and mobile UI/UX across 10 technology stacks.

133.1K 383.9K
View
writing-plans logo
Productivity & Planning

writing-plans

Comprehensive implementation plans for multi-step tasks, breaking down specs into bite-sized, testable steps.

295.4K 268K
View
using-superpowers logo
Productivity & Planning

using-superpowers

Introduction to the obra skills system with mandatory skill invocation rules and best practices.

295.4K 259.8K
View
executing-plans logo
Productivity & Planning

executing-plans

Execute a written implementation plan with critical review and task checkpoints.

295.4K 229.1K
View
dispatching-parallel-agents logo
Productivity & Planning

dispatching-parallel-agents

Delegate independent tasks to specialized agents working concurrently with isolated context.

295.4K 206.3K
View
using-git-worktrees logo
Productivity & Planning

using-git-worktrees

Isolated git worktrees with smart directory selection and safety verification.

295.4K 205.2K
View
webapp-testing logo
Productivity & Planning

webapp-testing

Toolkit for interacting with and testing local web applications using Playwright. Supports verifying frontend functionality, debugging UI behavior, capturing browser screenshots, and viewing browser logs.

179.7K 170.6K
View
content-strategy logo
Productivity & Planning

content-strategy

Plan searchable and shareable content that drives traffic, builds authority, and generates leads.

53.3K 150.9K
View
repo-intake-and-plan logo
Productivity & Planning

repo-intake-and-plan

README-first repository scanner that extracts commands and classifies reproduction candidates without executing them.

497 139.6K
View
marketing-ideas logo
Productivity & Planning

marketing-ideas

Brainstorm and prioritize marketing strategies tailored to your SaaS stage, budget, and goals.

53.3K 137.2K
View
site-architecture logo
Productivity & Planning

site-architecture

Plan and optimize your website's page hierarchy, navigation, URL structure, and internal linking.

53.3K 112.8K
View

Productivity AI tools

Vimcal logo
Productivity

Vimcal

The world's fastest calendar for remote work

Free
View
SaveDay logo
Productivity

SaveDay

Capture, organize, and utilize your knowledge effortlessly.

Free
View
Any Summary logo
Productivity

Any Summary

Instant Summaries of Audio & Video Interviews with AnySummary

Freemium
View
M
Productivity

Map This

Transform PDFs into engaging mind maps.

Freemium
View
ChatPDF logo
Productivity

ChatPDF

Chat with any PDF instantly

Free
View
I
Productivity

intellisay

Create an optimal daily plan using your voice

Paid
View
A
Productivity

Aurora AI

A productivity platform to centralize organizational knowledge and workflows with contextual AI assistance.

Paid
View
AskYourPDF logo
Productivity

AskYourPDF

AskYourPDF Pricing Plans: Tailored to Your Needs

Free
View