| author | |
| committer | |
| log | 7d955274bb40bc937275b3d59c6304dd44c85d40 |
| tree | e22f6f6d07fe18915ec2f954596caf79f71e11d3 |
| parent | 2e4a6c88b535f049fd52a84d9b9e510cf70a42fd |
2 files changed, 31 insertions(+), 14 deletions(-)
lib/init/src/main.zig+24-2| ... | ... | @@ -1,10 +1,32 @@ |
| 1 | 1 | const std = @import("std"); |
| 2 | const Io = std.Io; | |
| 3 | ||
| 2 | 4 | const _NAME = @import(".NAME"); |
| 3 | 5 | |
| 4 | 6 | pub fn main() !void { |
| 5 | // Prints to stderr, ignoring potential errors. | |
| 7 | // Prints to stderr, unbuffered, ignoring potential errors. | |
| 6 | 8 | 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! | |
| 8 | 30 | } |
| 9 | 31 | |
| 10 | 32 | test "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. | |
| 2 | 2 | const std = @import("std"); |
| 3 | const Io = std.Io; | |
| 3 | 4 | |
| 4 | pub 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. | |
| 8 | pub fn printAnotherMessage(writer: *Io.Writer) Io.Writer.Error!void { | |
| 9 | try writer.print("Run `zig build test` to run the tests.\n", .{}); | |
| 15 | 10 | } |
| 16 | 11 | |
| 17 | 12 | pub fn add(a: i32, b: i32) i32 { |