zig-0.16
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseZig 0.16-dev Porting Notes
Zig 0.16-dev 移植笔记
This skill captures gotchas hit when porting a 0.15 project to Zig 0.16-dev (std.Io world). APIs keep moving; always check the stdlib sources for your exact build.
本文记录了将Zig 0.15项目移植到Zig 0.16-dev(std.Io环境)时遇到的常见问题。API一直在迭代更新,请务必根据你使用的具体版本查看标准库源码。
Std.Io replaces std.net / legacy std.posix wrappers
Std.Io 替代 std.net / 旧版 std.posix 包装器
- is gone. The new networking lives under
std.net(types:std.Io.net,IpAddress,Stream,Server, etc.).UnixAddress - High-level functions like ,
tcpConnectToHost,Address.parseIp,Stream.connectno longer exist. You must useStream.read/writeAllvtables (Io,io.vtable.netConnectIp,netRead,netWrite,netListenIp).netAccept - If you need raw fds, call platform syscalls (,
std.os.linux.socket,accept4,connect,bind,listen,write,writev,read). Many helpers that were undercloseare gone.std.posix - ,
posix.socket,posix.bind,posix.accept,posix.shutdown,posix.writev,posix.listen,posix.pipe2,posix.getrandom, etc. are removed. Usestd.net.has_unix_socketssyscalls andstd.os.<platform>where available, or the Io vtable.posix.system.* - Epoll: use directly; signatures include an explicit
std.os.linux.epoll_create1/epoll_ctl/epoll_waitparam for writev/epoll_wait.count
- 已被移除。新的网络功能位于**
std.net**下(类型包括:std.Io.net、IpAddress、Stream、Server等)。UnixAddress - 诸如、
tcpConnectToHost、Address.parseIp、Stream.connect等高级函数已不存在。你必须使用Stream.read/writeAll虚表(Io、io.vtable.netConnectIp、netRead、netWrite、netListenIp)。netAccept - 如果需要原始文件描述符,请调用平台系统调用(、
std.os.linux.socket、accept4、connect、bind、listen、write、writev、read)。原close下的许多辅助函数已被移除。std.posix - 、
posix.socket、posix.bind、posix.accept、posix.shutdown、posix.writev、posix.listen、posix.pipe2、posix.getrandom等已被移除。请使用std.net.has_unix_sockets系统调用和可用的std.os.<platform>,或者Io虚表。posix.system.* - Epoll:直接使用;函数签名中
std.os.linux.epoll_create1/epoll_ctl/epoll_wait包含显式的writev/epoll_wait参数。count
Std.Io Readers/Writers
Std.Io 读取器/写入器
- New writer/reader interfaces live under . TLS expects
std.Io/*std.Io.Reader(struct fields*std.Io.Writer). Pass pointers to interfaces, not call.interfaceas a function.interface() - removed; use
std.time.sleep.std.Io.sleep(io, Duration, Clock)
- 新的写入器/读取器接口位于下。TLS需要
std.Io/*std.Io.Reader(结构体字段为*std.Io.Writer)。请传递接口指针,不要将.interface作为函数调用。interface() - 已被移除;请使用
std.time.sleep。std.Io.sleep(io, Duration, Clock)
Time / Random / Env / Exit
时间 / 随机数 / 环境变量 / 退出
- removed. Use
std.time.milliTimestamporstd.time.Timerand comparestd.Io.Clock.now(clock, io).Timestamp.nanoseconds - Random secure bytes: ; no
std.Io.randomSecure(io, buf)orstd.crypto.randomconvenience.std.posix.getrandom - is gone; use
std.process.getEnvVarOwnedand copy.std.c.getenv - removed; use
std.posix.exit.std.process.exit
- 已被移除。请使用
std.time.milliTimestamp或std.time.Timer并比较std.Io.Clock.now(clock, io)。Timestamp.nanoseconds - 生成安全随机字节:使用;不再提供
std.Io.randomSecure(io, buf)或std.crypto.random的便捷方法。std.posix.getrandom - 已被移除;请使用
std.process.getEnvVarOwned并自行复制。std.c.getenv - 已被移除;请使用
std.posix.exit。std.process.exit
TLS Client options
TLS 客户端选项
- now requires
std.crypto.tls.Client.Optionsandentropy: *const [entropy_len]u8. Fill entropy withrealtime_now_seconds: i64; compute seconds withstd.Io.randomSecure/Io.Clock.now(.real, io).ns_per_s
- 现在需要
std.crypto.tls.Client.Options和entropy: *const [entropy_len]u8参数。使用realtime_now_seconds: i64填充熵值;通过std.Io.randomSecure/Io.Clock.now(.real, io)计算秒数。ns_per_s
MemoryPool API changes
MemoryPool API 变更
- returns the pool;
std.heap.MemoryPool(T).initCapacity(allocator, n)/createnow require allocator. No baredestroyor zero-arginit().deinit()
- 返回内存池;
std.heap.MemoryPool(T).initCapacity(allocator, n)/create现在需要传入allocator参数。不再支持无参数的destroy或init()。deinit()
Atomic order & Thread
原子顺序与线程
- Atomic orders use /
.monotonic;.seq_cstremoved..awake
- 原子顺序使用/
.monotonic;.seq_cst已被移除。.awake
Format options
格式化选项
- replaces
std.fmt.Options.FormatOptions - helper is gone; call
std.fmt.formatdirectly (writers live inwriter.print).std.Io.Writer
- 替代了
std.fmt.Options。FormatOptions - 辅助函数已被移除;请直接调用
std.fmt.format(写入器位于writer.print中)。std.Io.Writer
Randomness / crypto changes
随机数 / 加密功能变更
- was removed. Use an
std.crypto.randominstance:std.Io.const io = std.Io.Threaded.global_single_threaded.ioBasic(); io.random(&buf); - now requires an
Ed25519.KeyPair.generateargument:io: std.Io.Ed25519.KeyPair.generate(io);
- 已被移除。请使用
std.crypto.random实例:std.Io。const io = std.Io.Threaded.global_single_threaded.ioBasic(); io.random(&buf); - 现在需要传入
Ed25519.KeyPair.generate参数:io: std.Io。Ed25519.KeyPair.generate(io);
Enum conversion
枚举转换
- removed. Use
std.meta.intToEnum(returnsstd.enums.fromInt(EnumType, value)).?EnumType
- 已被移除。请使用
std.meta.intToEnum(返回std.enums.fromInt(EnumType, value))。?EnumType
Fixed-buffer writers in tests
测试中的固定缓冲区写入器
- was removed. For in-memory writes use
std.io.fixedBufferStreamand read bytes withvar w = std.Io.Writer.fixed(buf);.std.Io.Writer.buffered(&w) - no longer has
std.ArrayListshorthand; use.init(allocator)for stack buffers or managed variants..initBuffer(slice)
- 已被移除。对于内存中的写入操作,请使用
std.io.fixedBufferStream并通过var w = std.Io.Writer.fixed(buf);读取字节。std.Io.Writer.buffered(&w) - 不再支持
std.ArrayList简写;对于栈缓冲区请使用.init(allocator),或使用托管变体。.initBuffer(slice)
Stream/Conn helpers we added (compat pattern)
我们添加的流/连接辅助工具(兼容模式)
- Build a thin compat layer that wraps platform syscalls to reintroduce and address parsing/formatting, plus
Stream { read, writeAll, close, readAtLeast }using raw sockets.tcpConnectToHost/Address - For Unix sockets: build sockaddr manually and call .
connect
- 构建一个轻量级兼容层,包装平台系统调用,重新实现以及地址解析/格式化,同时基于原始套接字实现
Stream { read, writeAll, close, readAtLeast }。tcpConnectToHost/Address - 对于Unix套接字:手动构建sockaddr并调用。
connect
Testing adjustments
测试调整
- For in-process client/server tests, use to avoid relying on Io vtable network (Threaded nets return
socketpair(AF.UNIX, SOCK.STREAM|CLOEXEC, 0, &fds)if not wired).NetworkDown
- 对于进程内客户端/服务器测试,请使用,避免依赖Io虚表网络(如果未配置,Threaded网络会返回
socketpair(AF.UNIX, SOCK.STREAM|CLOEXEC, 0, &fds))。NetworkDown
Error sets tightened
错误集收紧
- Many functions return narrower error sets (e.g., Reader.fill no longer includes /
WouldBlock). Remove unreachable branches accordingly.Timeout
- 许多函数返回更窄的错误集(例如Reader.fill不再包含/
WouldBlock)。请相应地移除不可达分支。Timeout
syscalls return types
系统调用返回类型
- return
std.os.linux.*; cast withusizebefore checking@as(isize, @bitCast(rc)). For< 0include thewritev/epoll_waitargument.count
Use this skill when upgrading 0.15-era code to 0.16-dev to avoid common build-breakers.
- 返回
std.os.linux.*;在检查usize之前,请使用< 0进行类型转换。对于@as(isize, @bitCast(rc)),请传入writev/epoll_wait参数。count
将0.15时代的代码升级到0.16-dev时,可参考本文避免常见的构建失败问题。