mirror of
https://github.com/ziglang/zig.git
synced 2024-11-26 23:22:44 +00:00
e3424332d3
* `doc/langref` formatting * upgrade `.{ .path = "..." }` to `b.path("...")` * avoid using arguments named `self` * make `Build.Step.Id` usage more consistent * add `Build.pathResolve` * use `pathJoin` and `pathResolve` everywhere * make sure `Build.LazyPath.getPath2` returns an absolute path
37 lines
842 B
Zig
37 lines
842 B
Zig
const std = @import("std");
|
|
const Allocator = std.mem.Allocator;
|
|
|
|
const Foo = struct { data: *u32 };
|
|
|
|
fn getData() !u32 {
|
|
return 666;
|
|
}
|
|
|
|
fn genFoos(allocator: Allocator, num: usize) ![]Foo {
|
|
const foos = try allocator.alloc(Foo, num);
|
|
errdefer allocator.free(foos);
|
|
|
|
// Used to track how many foos have been initialized
|
|
// (including their data being allocated)
|
|
var num_allocated: usize = 0;
|
|
errdefer for (foos[0..num_allocated]) |foo| {
|
|
allocator.destroy(foo.data);
|
|
};
|
|
for (foos, 0..) |*foo, i| {
|
|
foo.data = try allocator.create(u32);
|
|
num_allocated += 1;
|
|
|
|
if (i >= 3) return error.TooManyFoos;
|
|
|
|
foo.data.* = try getData();
|
|
}
|
|
|
|
return foos;
|
|
}
|
|
|
|
test "genFoos" {
|
|
try std.testing.expectError(error.TooManyFoos, genFoos(std.testing.allocator, 5));
|
|
}
|
|
|
|
// test
|