Skip to content

Commit cf50bd5

Browse files
committed
Use std.process.Init
1 parent 7e94b0a commit cf50bd5

4 files changed

Lines changed: 9 additions & 16 deletions

File tree

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,11 @@ const Config = struct {
7171
peers: []const Address,
7272
};
7373
74-
pub fn main() anyerror!void {
75-
var parser = toml.Parser(Config).init(allocator);
74+
pub fn main(init: std.process.Init) anyerror!void {
75+
var parser = toml.Parser(Config).init(init.gpa);
7676
defer parser.deinit();
7777
78-
var result = try parser.parseFile("./examples/example1.toml");
78+
var result = try parser.parseFile(init.io, "./examples/example1.toml");
7979
defer result.deinit();
8080
8181
const config = result.value;

examples/example1.zig

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
const std = @import("std");
22
const toml = @import("toml");
33

4-
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
5-
const allocator = gpa.allocator();
6-
74
const Address = struct {
85
port: i64,
96
host: []const u8,
@@ -18,11 +15,11 @@ const Config = struct {
1815
peers: []const Address,
1916
};
2017

21-
pub fn main() anyerror!void {
22-
var parser = toml.Parser(Config).init(allocator);
18+
pub fn main(init: std.process.Init) anyerror!void {
19+
var parser = toml.Parser(Config).init(init.gpa);
2320
defer parser.deinit();
2421

25-
var result = try parser.parseFile("./examples/example1.toml");
22+
var result = try parser.parseFile(init.io, "./examples/example1.toml");
2623
defer result.deinit();
2724

2825
const config = result.value;

src/root.zig

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,7 @@ pub fn Parser(comptime Target: type) type {
9898
}
9999
}
100100

101-
pub fn parseFile(self: *Self, filename: []const u8) !Parsed(Target) {
102-
var threaded = std.Io.Threaded.init(self.alloc, .{});
103-
const io = threaded.io();
104-
defer threaded.deinit();
105-
101+
pub fn parseFile(self: *Self, io: std.Io, filename: []const u8) !Parsed(Target) {
106102
const file = try std.Io.Dir.cwd().openFile(io, filename, .{});
107103
defer file.close(io);
108104

src/tests.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ test "parse into struct" {
9494
var p = main.Parser(Aa).init(testing.allocator);
9595
defer p.deinit();
9696

97-
const parsed = try p.parseFile("./test/doc1.toml.txt");
97+
const parsed = try p.parseFile(std.testing.io, "./test/doc1.toml.txt");
9898
const aa: Aa = parsed.value;
9999
defer parsed.deinit();
100100

@@ -199,7 +199,7 @@ test "deinit table" {
199199
var p = main.Parser(main.Table).init(testing.allocator);
200200
defer p.deinit();
201201

202-
const parsed = try p.parseFile("./test/doc1.toml.txt");
202+
const parsed = try p.parseFile(std.testing.io, "./test/doc1.toml.txt");
203203
_ = parsed.value;
204204
defer parsed.deinit();
205205
}

0 commit comments

Comments
 (0)