authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-12-19 19:15:05-08:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-12-23 22:15:11-08:00
log7d955274bb40bc937275b3d59c6304dd44c85d40
treee22f6f6d07fe18915ec2f954596caf79f71e11d3
parent2e4a6c88b535f049fd52a84d9b9e510cf70a42fd

update the init templates to new std API


2 files changed, 31 insertions(+), 14 deletions(-)

lib/init/src/main.zig+24-2
......@@ -1,10 +1,32 @@
11const std = @import("std");
2const Io = std.Io;
3
24const _NAME = @import(".NAME");
35
46pub fn main() !void {
5 // Prints to stderr, ignoring potential errors.
7 // Prints to stderr, unbuffered, ignoring potential errors.
68 std.debug.print("All your {s} are belong to us.\n", .{"codebase"});
7 try _NAME.bufferedPrint();
9
10 // In order to allocate memory we must construct an `Allocator` instance.
11 var debug_allocator: std.heap.DebugAllocator(.{}) = .init;
12 defer _ = debug_allocator.deinit(); // This checks for leaks.
13 const gpa = debug_allocator.allocator();
14
15 // In order to do I/O operations we must construct an `Io` instance.
16 var threaded: std.Io.Threaded = .init(gpa);
17 defer threaded.deinit();
18 const io = threaded.io();
19
20 // Stdout is for the actual output of your application, for example if you
21 // are implementing gzip, then only the compressed bytes should be sent to
22 // stdout, not any debugging messages.
23 var stdout_buffer: [1024]u8 = undefined;
24 var stdout_file_writer: Io.File.Writer = .init(.stdout(), io, &stdout_buffer);
25 const stdout_writer = &stdout_file_writer.interface;
26
27 try _NAME.printAnotherMessage(stdout_writer);
28
29 try stdout_writer.flush(); // Don't forget to flush!
830}
931
1032test "simple test" {
lib/init/src/root.zig+7-12
......@@ -1,17 +1,12 @@
1//! By convention, root.zig is the root source file when making a library.
1//! By convention, root.zig is the root source file when making a package.
22const std = @import("std");
3const Io = std.Io;
34
4pub fn bufferedPrint() !void {
5 // Stdout is for the actual output of your application, for example if you
6 // are implementing gzip, then only the compressed bytes should be sent to
7 // stdout, not any debugging messages.
8 var stdout_buffer: [1024]u8 = undefined;
9 var stdout_writer = std.fs.File.stdout().writer(&stdout_buffer);
10 const stdout = &stdout_writer.interface;
11
12 try stdout.print("Run `zig build test` to run the tests.\n", .{});
13
14 try stdout.flush(); // Don't forget to flush!
5/// This is a documentation comment to explain the `printAnotherMessage` function below.
6///
7/// Accepting an `Io.Writer` instance is a handy way to write reusable code.
8pub fn printAnotherMessage(writer: *Io.Writer) Io.Writer.Error!void {
9 try writer.print("Run `zig build test` to run the tests.\n", .{});
1510}
1611
1712pub fn add(a: i32, b: i32) i32 {