Nim Ownership Hooks logo

Nim Ownership Hooks

Community
planetis-m
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`, `=sink`, or move semantics.

Overview

Publisherplanetis-m
Repositoryskills
Skill namenim-ownership-hooks
Stars
66
Forks
7
Bundled files
4
Links
  • Markdown instructions

    A SKILL.md file the model loads on demand, so it only costs tokens when a request actually matches.

  • Works with any LLM

    AI skills are plain Markdown, not provider-specific code, so this works with GPT, Claude, Gemini, Grok, or a local model.

  • 4 bundled files

    Scripts, templates, and references the model can read while it works. Files are read-only and never executed.

  • Open source

    Published by planetis-m on GitHub. Read the source before you install it.

Installation

Install the Nim Ownership Hooks AI skill in TypingMind to use it with any LLM, or drop it into another agent that reads SKILL.md.

1

Install in TypingMind

TypingMind installs a skill straight from its GitHub folder — it reads SKILL.md, bundles the resource files, and stores the result locally.

  1. Open the app and go to Plugins → Skills.
  2. Choose "Install from GitHub".
  3. Paste the skill folder URL below and confirm.
    https://github.com/planetis-m/skills/tree/master/nim-ownership-hooks
  4. Enable the skill in any chat where you want it available.
Plugins → Skills → Add skill → From GitHub URL, then paste the folder URL and press Continue.
2

Install in another agent

Any agent that reads the Agent Skills format can use this skill — copy the folder into that agent's skills directory.

Claude Code — .claude/skills
git clone --depth 1 https://github.com/planetis-m/skills.git /tmp/skills
mkdir -p .claude/skills
cp -r /tmp/skills/nim-ownership-hooks .claude/skills/nim-ownership-hooks
Restart Claude Code after copying so it picks up the new skill.

Use it in TypingMind

Enable Nim Ownership Hooks in any TypingMind chat and the model takes it from there. Its name and description sit in the system prompt, and the moment a request matches, the model loads the full instructions itself — you never invoke it by hand, and it costs no tokens until it is actually used.

The model loads Nim Ownership Hooks on its own as soon as a request matches it.

Works with any AI model

AI skills are plain Markdown instructions rather than provider-specific code, so Nim Ownership Hooks is not tied to the model it was written for. Install it once in TypingMind and use it with GPT-5, Claude, Gemini, Grok, DeepSeek, Mistral, Llama, or a local model you run yourself — all on your own API keys.

  • Loaded only when it is needed

    The system prompt carries just the name and description. The instructions are fetched on the first matching request, so an idle skill costs nothing.

  • Switch models mid-chat

    Because the skill is instructions rather than code, changing model does not break it — the next model reads the same SKILL.md.

Skill instructions

This is the SKILL.md content the model loads. Read it before installing — a skill is instructions your model will follow.

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

  • 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

  • Define a valid moved-from state. Reset only the fields needed by =destroy and the operations permitted in that state.
  • 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

  • Move-only types: add =dup {.error.}.
  • Deep-owning raw-storage containers: mark =dup with {.nodestroy.}. 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.
  • Refcounted types: increment the counter and share the pointer. No {.nodestroy.} needed unless the hook writes into raw uninitialized storage.

=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

  • sink parameters are affine, not linear: the callee may consume the value once, or not at all.
  • When the compiler cannot prove a sink argument is last use, it inserts =copy or =dup before passing. See nim-api-design for the caller-facing rules on when move() is required.
  • move(x) leaves the source alive and moved-from; the source's =destroy still runs.
  • ensureMove(x) makes the source dead: later use is a compile error, and the source's =destroy is skipped. Use it for return positions and must-fail-instead-of-copy cases, not routine sink arguments.
  • =wasMoved(x) followed by =destroy(x) cancel: the destroy is eliminated.

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").

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.}, =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:

  • 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)
  • Test every operation the type permits on a moved-from value
  • Self-copy (x = x does not crash)
  • Sink from temporaries
  • Zero-length initialization (if the type has constructors)

Common Mistakes

MistakeWhy it is wrong
Setting fields to nil inside =destroyMixes resource release with moved-from state handling.
Using move when ensureMove would compileAccidental use after the move is not rejected.
alloc in multi-threaded codeCross-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

  • references/move_only_owner.md — Hooks for exclusive ownership with copying forbidden.
  • references/deep_owning_container.md — Destroy, copy, and duplicate hooks for raw storage with independent copies.
  • references/shared_refcounted.md — Separate-counter and generic SharedPtr patterns for shared ownership.

Bundled files

The model reads these on demand while the skill is loaded. They are exposed as readable files and are never executed.

Frequently asked questions

What does the Nim Ownership Hooks AI skill do?

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`, `=sink`, or move semantics.

Why use Nim Ownership Hooks on TypingMind?

Because you install it once and use it with any model. Nim Ownership Hooks is plain Markdown rather than provider-specific code, so the same skill runs on GPT-5, Claude, Gemini, Grok, or a local model — and you can switch model mid-chat without it breaking. TypingMind runs on your own API keys, so you pay providers directly instead of a per-seat subscription, and your skills and chats stay in your own storage.

How do I install Nim Ownership Hooks in TypingMind?

Open Plugins → Skills → Install from GitHub in TypingMind and paste https://github.com/planetis-m/skills/tree/master/nim-ownership-hooks. TypingMind reads its SKILL.md and bundles its files and installs it as a skill you can enable per chat.

Which AI models can use Nim Ownership Hooks?

Any model you connect in TypingMind. AI skills are plain Markdown instructions rather than provider-specific code, so GPT, Claude, Gemini, Grok, and local models can all load this skill when a request matches it.

How many AI models can I use with Nim Ownership Hooks?

As many as you like. As long as a model supports skills, you can use Nim Ownership Hooks with it — GPT, Claude, Gemini, Grok, DeepSeek, Mistral, Llama and more — all on TypingMind with your own API keys.

Is the Nim Ownership Hooks AI skill free?

It is published on GitHub by planetis-m. Check the repository for licensing terms. You only pay your own AI provider for the tokens you use.

What are AI skills?

An AI skill is a reusable instruction bundle that teaches an AI model how to do one specific task. It follows the open Agent Skills format: a SKILL.md file with a name and description, plus any scripts, templates or reference files the model may need. The model reads the instructions only when your request matches the skill, so an installed skill costs nothing until it is used.

How are AI skills different from plugins or MCP servers?

A plugin or MCP server gives a model new tools to call — code that runs somewhere and returns a result. An AI skill gives the model knowledge and process instead: how to approach a task, which steps to follow, what good output looks like. Skills are plain Markdown, so they need no server, no API key and no runtime, and they work with any model.

View all

Set up your own AI workspace now

Get notified about new features and future giveaways by subscribing to our newsletter 👇