From 7d955274bb40bc937275b3d59c6304dd44c85d40 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Fri, 19 Dec 2025 19:15:05 -0800 Subject: [PATCH] update the init templates to new std API --- lib/init/src/main.zig | 26 ++++++++++++++++++++++++-- lib/init/src/root.zig | 19 +++++++------------ 2 files changed, 31 insertions(+), 14 deletions(-) diff --git a/lib/init/src/main.zig b/lib/init/src/main.zig index 88dd8348e1ff4ff9fdad70126d0f2b075428312a..9df441b56468877f4f8a1a1869827fb6b4c44677 100644 --- a/lib/init/src/main.zig +++ b/lib/init/src/main.zig @@ -1,10 +1,32 @@ const std = @import("std"); +const Io = std.Io; + const _NAME = @import(".NAME"); pub fn main() !void { - // Prints to stderr, ignoring potential errors. + // Prints to stderr, unbuffered, ignoring potential errors. std.debug.print("All your {s} are belong to us.\n", .{"codebase"}); - try _NAME.bufferedPrint(); + + // In order to allocate memory we must construct an `Allocator` instance. + var debug_allocator: std.heap.DebugAllocator(.{}) = .init; + defer _ = debug_allocator.deinit(); // This checks for leaks. + const gpa = debug_allocator.allocator(); + + // In order to do I/O operations we must construct an `Io` instance. + var threaded: std.Io.Threaded = .init(gpa); + defer threaded.deinit(); + const io = threaded.io(); + + // Stdout is for the actual output of your application, for example if you + // are implementing gzip, then only the compressed bytes should be sent to + // stdout, not any debugging messages. + var stdout_buffer: [1024]u8 = undefined; + var stdout_file_writer: Io.File.Writer = .init(.stdout(), io, &stdout_buffer); + const stdout_writer = &stdout_file_writer.interface; + + try _NAME.printAnotherMessage(stdout_writer); + + try stdout_writer.flush(); // Don't forget to flush! } test "simple test" { diff --git a/lib/init/src/root.zig b/lib/init/src/root.zig index 94c7cd01194b2195b8f5c866fa640fc90a07bb76..5a7125032bf25a4262eca6166cd8d5e2a246a4a1 100644 --- a/lib/init/src/root.zig +++ b/lib/init/src/root.zig @@ -1,17 +1,12 @@ -//! By convention, root.zig is the root source file when making a library. +//! By convention, root.zig is the root source file when making a package. const std = @import("std"); +const Io = std.Io; -pub fn bufferedPrint() !void { - // Stdout is for the actual output of your application, for example if you - // are implementing gzip, then only the compressed bytes should be sent to - // stdout, not any debugging messages. - var stdout_buffer: [1024]u8 = undefined; - var stdout_writer = std.fs.File.stdout().writer(&stdout_buffer); - const stdout = &stdout_writer.interface; - - try stdout.print("Run `zig build test` to run the tests.\n", .{}); - - try stdout.flush(); // Don't forget to flush! +/// This is a documentation comment to explain the `printAnotherMessage` function below. +/// +/// Accepting an `Io.Writer` instance is a handy way to write reusable code. +pub fn printAnotherMessage(writer: *Io.Writer) Io.Writer.Error!void { + try writer.print("Run `zig build test` to run the tests.\n", .{}); } pub fn add(a: i32, b: i32) i32 { -- 2.54.0