Nim ownership hooks

From planetis-m/skills · View interactive version

Nim ownership hooks is an AI agent skill from the GitHub repository planetis-m/skills. 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`, `=s

How to use this skill

Skill file

Nim Ownership Hooks

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.

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.

Export custom hooks for exported types.

Hook-by-hook rules

=destroy

=wasMoved

=sink

=copy

=dup

The copied elements are written into raw memory that does not hold valid old

values yet. {.nodestroy.} prevents implicit hook calls on those destination

slots. Use child =dup for elements with hooks.

{.nodestroy.} needed unless the hook writes into raw uninitialized storage.

=trace

lent T

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

passing. See nim-api-design for the caller-facing rules on when move() is required.

=destroy is skipped. Use it for return positions and must-fail-instead-of-copy cases, not

routine sink arguments.

Edge cases

Workflow

Step 1: Classify the ownership model

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

| Model | Hooks needed |

|-------|-------------|

| Plain / auto-managed | None |

| Borrowing / view | None. Use lent T for accessors. |

| Move-only owner | =destroy, =wasMoved, =copy as {.error.}, =dup 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:

Common Mistakes

| Mistake | Why it is wrong |

|---------|-----------------|

| Setting fields to nil inside =destroy | Mixes resource release with moved-from state handling. |

| Using move when ensureMove would compile | Accidental use after the move is not rejected. |

| alloc in multi-threaded code | Cross-thread ownership may access storage through the wrong allocator. |

| Skipping =dup on a move-only type | =dup can silently return a moved-from value instead of failing. |

References

independent copies.

ownership.

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.