Skip to content

Commit 0c9093d

Browse files
authored
Fix build (#1)
* add build module * pass through options * update readme
1 parent 09e5e08 commit 0c9093d

File tree

2 files changed

+80
-7
lines changed

2 files changed

+80
-7
lines changed

README.md

Lines changed: 68 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,39 @@ the two as needed. See the [Small Capacity Limit](#small-capacity-limit) for
88
details on this storage mechanism.
99

1010
```zig
11+
// We'll just use SmallArrayList for this example, but there are variants
12+
// that allow further parameterization.
13+
const SmallArrayList = @import("small_array_list").SmallArrayList;
14+
15+
// Setup whichever allocator you'd like to use.
16+
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
17+
defer arena.deinit();
18+
const allocator = arena.allocator();
19+
1120
var list: SmallArrayList(i32) = .empty;
1221
defer list.deinit(allocator);
1322
14-
list.append(allocator, 1);
15-
list.append(allocator, 2);
16-
list.append(allocator, 3);
23+
// Append works just like the std.array_list.ArrayListUnmanaged append.
24+
try list.append(allocator, 1);
1725
18-
std.debug.print("len={} capacity={}\n", .{ list.len, list.capacity });
19-
// On 64-bit systems, this will be: len=3 capacity=4
26+
// Nearly all of the standard library methods are available.
27+
var many = try list.addManyAsArray(allocator, 2);
28+
many[0] = 2;
29+
many[1] = 3;
2030
31+
// Unlike ArrayList, items cannot be accessed directly as a slice.
2132
for (list.items()) |item| {
22-
std.debug.print("item={}\n", .{ item });
33+
std.debug.print("item={}\n", .{item});
2334
}
2435
36+
// You can see if the SmallArrayList is using an allocation or not.
2537
if (!list.hasAllocation()) {
2638
std.debug.print("no allocations!\n", .{});
2739
}
40+
41+
// Unlike ArrayList, len is a top-level field.
42+
// On 64-bit systems, this will be: len=3 capacity=4
43+
std.debug.print("len={} capacity={}\n", .{ list.len, list.capacity });
2844
```
2945

3046
> [!IMPORTANT]
@@ -34,6 +50,52 @@ if (!list.hasAllocation()) {
3450
> 1. To get the number of items in the list, use `list.len` instead of `list.items.len`.
3551
> 1. To get the items of in the list, use `list.items()` instead of `list.items`.
3652
53+
## Installing
54+
55+
First, add the dependency to your `build.zig.zon`:
56+
57+
```sh
58+
zig fetch --save git+https://github.com/kalamay/small-array-list#v0.1.1
59+
```
60+
61+
Next add the dependecy to your `build.zig`:
62+
63+
```zig
64+
const small_array_list_mod = b.dependency("small_array_list", .{
65+
.target = target,
66+
.optimize = optimize,
67+
}).module("small_array_list");
68+
69+
exe_mod.addImport("small_array_list", small_array_list_mod);
70+
// or lib_mod, or any other module
71+
```
72+
73+
Now you can import the library:
74+
75+
```zig
76+
const small_array_list = @import("small_array_list");
77+
```
78+
79+
### Using a Different Name
80+
81+
If you'd like to use a different name within your project, you can choose a
82+
different import name for the module:
83+
84+
```zig
85+
const small_array_list_mod = b.dependency("small_array_list", .{
86+
.target = target,
87+
.optimize = optimize,
88+
}).module("small_array_list");
89+
90+
exe_mod.addImport("array", small_array_list_mod);
91+
```
92+
93+
And then you'd be able to import it as `"array"`:
94+
95+
```zig
96+
const array = @import("array");
97+
```
98+
3799
## Small Capacity Limit
38100

39101
The available small capacity limit is determined by the type stored and the

build.zig

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,23 @@
11
const std = @import("std");
22

33
pub fn build(b: *std.Build) void {
4+
const root_source_file = "src/root.zig";
5+
46
const unit_tests = b.addTest(.{
5-
.root_source_file = b.path("src/root.zig"),
7+
.root_source_file = b.path(root_source_file),
68
});
79

810
const run_unit_tests = b.addRunArtifact(unit_tests);
911

1012
const test_step = b.step("test", "Run unit tests");
1113
test_step.dependOn(&run_unit_tests.step);
14+
15+
const target = b.standardTargetOptions(.{});
16+
const optimize = b.standardOptimizeOption(.{});
17+
18+
_ = b.addModule("small_array_list", .{
19+
.root_source_file = b.path(root_source_file),
20+
.target = target,
21+
.optimize = optimize,
22+
});
1223
}

0 commit comments

Comments
 (0)