Nim c wrappers

From planetis-m/skills · View interactive version

Nim c wrappers is an AI agent skill from the GitHub repository planetis-m/skills. Turn low-level C FFI bindings into an idiomatic Nim API, with safer types, cleaner module boundaries, resource management, callbacks, enums, bitflags, and exception-based error handling. Use when building a high-level Nim wrapper around a C library or improving the usability of existing raw bindings

How to use this skill

Skill file

Nim C Wrappers

Rules

Architecture

Raw FFI Layer

Type Mapping

| C Type | Nim Type | Notes |

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

| char | cchar | Platform-dependent signedness |

| signed char | cschar | Always int8 |

| unsigned char | uint8 | Safer default spelling; avoid relying on cuchar status across Nim versions |

| short | cshort | Always int16 |

| unsigned short | cushort | Always uint16 |

| int | cint | Always int32 |

| unsigned int | cuint | Always uint32 |

| long | clong | ABI-sized (4 on Win64, 8 on LP64) |

| unsigned long | culong | ABI-sized |

| long long | clonglong | Always int64 |

| unsigned long long | culonglong | Always uint64 |

| size_t | csize_t | Alias for uint |

| intptr_t | int | Pointer-sized signed |

| uintptr_t | uint | Pointer-sized unsigned |

| float | cfloat | Always float32 |

| double | cdouble | Always float64 |

| long double | clongdouble | Limited codegen support |

| void* | pointer | Untyped |

| T* | ptr T | Nullable |

| T** | ptr ptr T | |

| char* / const char* | cstring | NUL-terminated |

| char** | cstringArray | ptr UncheckedArray[cstring] |

Struct types: object in C order. Fixed arrays: array[N, T]. Pointer+length: ptr T + csize_t. Raw buffers: ptr UncheckedArray[T]. Opaque handles: pointer or ptr OpaqueObj (empty object).

Ergonomic Layer

Naming

Callbacks

Verification

Workflow

  1. Read the C API. Identify structs, enums, functions, callbacks, and ownership semantics.
  2. Map types. Use the type table. Verify sizes with static: doAssert sizeof.
  3. Write raw bindings. One module per C header or subsystem. Centralize shared raw types to avoid cycles. Use importc, correct calling convention, packed only when C specifies it. Use typed integer aliases for enums.
  4. Write ergonomic wrappers. Add destructors for owned resources (move-only or RC). Raise exceptions for C errors. Re-export only the intended public surface. Strip prefixes.
  5. Test. Compile check → link check → smoke test → ABI sizeof/offset asserts → runtime ownership tests.
  6. Iterate. Add missing APIs as needed. Keep raw layer stable.

Common Mistakes

| Mistake | Why it's wrong |

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

| Passing Nim string directly to char* parameter | Type mismatch; use .cstring |

| Using seq where C expects stable pointer | add() can reallocate, invalidating the pointer |

| Reordering struct fields | Breaks ABI layout, causes garbage values |

| Using Nim enum for C enum values | ABI incompatibility; use typed integer aliases |

| Adding enum or constant values without inspecting headers | Find actual enum values in C headers, do not guess |

| Passing closures as C callbacks | C expects plain function pointers, not GC closures |

| Storing cstring beyond the call | cstring from string.cstring dangles after the source is freed |

| =dup body using result = src in RC pattern | May trigger implicit =copy instead of =dup; use field-by-field assignment |

| Wrong calling convention | Causes crashes at call sites |

References

management.

bitwise flags.

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.