zig-0.15 breaking change 集

に公開

https://ziglang.org/download/0.15.1/release-notes.html

遭遇した breaking change を都度記録していく

zig-0.15.1

https://zenn.dev/smallkirby/articles/feb8ceefaddbd0#writergate

https://qiita.com/ktz_alias/items/193aa985700af817598c

ひっそりと 0.15.2

https://github.com/ziglang/zig/milestone/29?closed=1

Writer 系

std ライブラリーの IO の刷新が来ていて、
名付けて
Writergate

async I/O 復活の布石らしい。

  • writer try stdout.flush(); // Don't forget to flush!

fmt

ドキュメント

https://ziglang.org/documentation/master/std/#std.Io.Writer.print

format method 呼び出しに "{f}" が必要

https://ziglang.org/download/0.15.1/release-notes.html#f-Required-to-Call-format-Methods

allocPrintZ

allocPrintZ -> allocPrintSentinel with sentinel: 0

https://github.com/spiraldb/ziggy-pydust/pull/536

ArrayList(u8) から writer を作る件

    var out_buffer = std.ArrayList(u8).init(allocator);
    defer out_buffer.deinit();
    var writer = out_buffer.writer();

👇

    var out_buffer = std.array_list.Managed(u8).init(allocator);
    defer out_buffer.deinit();
    var buf: [1024]u8 = undefined;
    var adapter = out_buffer.writer().adaptToNewApi(&buf);
    // std.io.Writer を値渡しすると壊れる
    var writer: *std.io.Writer = &adapter.new_interface;

allocator から直接。

    var out = std.Io.Writer.Allocating.init(allocator);
    defer out.deinit();
    // std.io.Writer を値渡しすると壊れる
    var writer: *std.io.Writer = &out.writer;

https://ziglang.org/documentation/0.15.1/std/#std.Io.Writer

vtable

std.json.stringify

https://github.com/the-argus/zig-compile-commands より。

fn writeCompileCommands(file: *std.fs.File, compile_commands: []CompileCommandEntry) !void {
    const options = std.json.StringifyOptions{
        .whitespace = .indent_tab,
        .emit_null_optional_fields = false,
    };

    try std.json.stringify(compile_commands, options, file.*.writer());
}

👇

fn writeCompileCommands(file: *std.fs.File, compile_commands: []CompileCommandEntry) !void {
    var buf: [1024]u8 = undefined;
    var writer = file.*.writer(&buf);
    var s = std.json.Stringify{
        .writer = &writer.interface,
        .options = .{
            .whitespace = .indent_tab,
            .emit_null_optional_fields = false,
        },
    };
    try s.write(compile_commands);
    try writer.interface.flush(); // 必要 !
}

https://github.com/ziglang/zig/commit/e43617e686f62af55d6663b695ec725e21bff210

Reader系

readUntilDelimiterOrEofAlloc

takeDelimiterExclusive

https://ziggit.dev/t/0-15-1-reader-writer/11614

std.ArrayList

https://ziglang.org/download/0.15.1/release-notes.html#ArrayList-make-unmanaged-the-default

std.array_list.Managed

usingnamespace 消滅

https://ziglang.org/download/0.15.1/release-notes.html#usingnamespace-Removed

addTranslateC で zig module を生成するように書き換える。

// c.zig
pub usingnamespace @cImport({
    @cInclude("EGL/egl.h");
    @cInclude("GLES/gl.h");
    @cInclude("android/choreographer.h");
    @cInclude("android/log.h");
    @cInclude("android/sensor.h");
    @cInclude("android/set_abort_message.h");
    @cInclude("android_native_app_glue.h");
});

👇

// c.h include をまとめた c の header を作る
#include <EGL/egl.h>;
#include <GLES/gl.h>;
#include <android/choreographer.h>;
#include <android/log.h>;
#include <android/sensor.h>;
#include <android/set_abort_message.h>;
#include <android_native_app_glue.h>;
    // translate して zig module 化
    const t = b.addTranslateC(.{
        .target = target,
        .optimize = optimize,
        .root_source_file = b.path("src/c.h"),
    });
    t.addIncludePath(.{ .cwd_relative = b.fmt("{s}/sources/android/native_app_glue", .{apk.ndk.path}) });
    // import できるようにする
    exe.root_module.addImport("c", t.createModule());
const c = @import("c.zig");
// 👇 
const c = @import("c");

addLibrary (addSharedLibrary と addStaticLibrary 消滅)

    const exe = b.addSharedLibrary(.{
        .name = "native-activity",
        .target = target,
        .optimize = optimize,
        .root_source_file = b.path("src/main.zig"),
        .link_libc = true,
    });

👇

    const exe = b.addLibrary(.{
        .name = "native-activity",
        .root_module = b.addModule("native-activity", .{
            .target = target,
            .optimize = optimize,
            .root_source_file = b.path("src/main.zig"),
            .link_libc = true,
        }),
        .linkage = .dynamic,
    });

target

isWasm => cpu.arch.isWasm

debugger

codelldb でローカル変数の中身が表示できない

https://ziggit.dev/t/cant-print-local-variables-when-debug-with-lldb/10689/9

    const exe = b.addExecutable(.{
        .name = "hoge",
        .root_module = mod,
        // for debug break point
        .use_llvm = true,
    });

Discussion