aiskillstore/marketplace / skills/0xbigboss/zig-best-practices
zig-best-practices
VerifiedUse when reading or writing Zig files (.zig, build.zig, build.zig.zon).
| File | Kind | Size |
|---|---|---|
| C-INTEROP.md | markdown | 2.0 KB |
| DEBUGGING.md | markdown | 1.6 KB |
| GENERICS.md | markdown | 1.7 KB |
| SKILL.md | skill md | 3.3 KB |
| skill-report.json | config | 31.9 KB |
GENERICS.md
View on GitHub# Generic Data Structures in Zig
Use comptime type parameters to create reusable generic containers. Return a type from a function to build type-safe collections.
## When to Use
- Implementing custom containers (queues, stacks, trees)
- Building type-safe wrappers around allocations
- Creating domain-specific collections
## Pattern: Type-Returning Function
```zig
pub fn Queue(comptime Child: type) type {
return struct {
const Self = @This();
const Node = struct {
data: Child,
next: ?*Node,
};
allocator: std.mem.Allocator,
start: ?*Node,
end: ?*Node,
pub fn init(allocator: std.mem.Allocator) Self {
return Self{ .allocator = allocator, .start = null, .end = null };
}
pub fn enqueue(self: *Self, value: Child) !void {
const node = try self.allocator.create(Node);
node.* = .{ .data = value, .next = null };
if (self.end) |end| end.next = node else self.start = node;
self.end = node;
}
pub fn dequeue(self: *Self) ?Child {
const start = self.start orelse return null;
defer self.allocator.destroy(start);
if (start.next) |next| self.start = next else {
self.start = null;
self.end = null;
}
return start.data;
}
};
}
```
## Key Techniques
- `@This()` returns the enclosing struct type for self-reference
- Nested `Node` struct keeps implementation details private
- Allocator passed to init, stored for later operations
- `defer` for cleanup in dequeue prevents leaks
## Usage
```zig
var queue = Queue(u32).init(allocator);
try queue.enqueue(42);
const value = queue.dequeue(); // ?u32
```
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| slack-gif-creatoranthropics/skillsKnowledge and utilities for creating animated GIFs optimized for Slack. Provides constraints, validation tools, and animation concepts. Use when users request animated GIFs for Slack like "make me a GIF of X doing Y for Slack." | Stars: 198k | Updated | Claude CodeCodex | Verified | Install |
| ponytailDietrichGebert/ponytailLazy senior dev mode for any coding task: the smallest change that fully solves it, and a reply a busy human understands in one read. | Stars: 194k | Updated | Claude CodeCodex | Verified | Install |
| ponytail-debtDietrichGebert/ponytailHarvest every shortcut: comment into one debt ledger, so deferrals get tracked instead of forgotten. One-shot report. | Stars: 194k | Updated | Claude CodeCodex | Verified | Install |
| ponytail-debtDietrichGebert/ponytailList every `shortcut:` comment (and older `ponytail:` ones) as a debt ledger. One-shot report, changes nothing. Use for "ponytail debt", "what did ponytail defer", "list the shortcuts", /ponytail-debt. | Stars: 194k | Updated | Claude CodeCodex | Verified | Install |