zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit f27bd87ade2e43dce66963f43ce678e361ddbfc2 (tree)
parent 6f00157e1e0159a5115a4e9e2ed389e819499282
Author: Andrew Kelley <andrew@ziglang.org>
Date:   Thu, 18 Dec 2025 23:05:51 -0800

std.Io.Threaded: allow length-0 file writes

At first I thought about keeping this as an assertion but I can see this
being useful if you already know how many bytes to read and you are
filling the end of the buffer.

This also more closely mirrors POSIX APIs.

Diffstat:
Mlib/std/Io/File.zig | 4+---
Mlib/std/Io/Threaded.zig | 4++++
2 files changed, 5 insertions(+), 3 deletions(-)

diff --git a/lib/std/Io/File.zig b/lib/std/Io/File.zig @@ -497,7 +497,7 @@ pub fn setTimestampsNow(file: File, io: Io) SetTimestampsError!void { pub const ReadPositionalError = Reader.Error || error{Unseekable}; -/// Returns 0 on end of stream. +/// Returns 0 on stream end or if `buffer` has no space available for data. /// /// See also: /// * `reader` @@ -507,8 +507,6 @@ pub fn readPositional(file: File, io: Io, buffer: []const []u8, offset: u64) Rea pub const WritePositionalError = Writer.Error || error{Unseekable}; -/// Returns 0 on end of stream. -/// /// See also: /// * `writer` pub fn writePositional(file: File, io: Io, buffer: []const []const u8, offset: u64) WritePositionalError!usize { diff --git a/lib/std/Io/Threaded.zig b/lib/std/Io/Threaded.zig @@ -6400,6 +6400,7 @@ fn fileReadStreamingPosix(userdata: ?*anyopaque, file: File, data: []const []u8) i += 1; } } + if (i == 0) return 0; const dest = iovecs_buffer[0..i]; assert(dest[0].len > 0); @@ -6482,6 +6483,7 @@ fn fileReadStreamingWindows(userdata: ?*anyopaque, file: File, data: []const []u const DWORD = windows.DWORD; var index: usize = 0; while (data[index].len == 0) index += 1; + if (index == 0) return 0; const buffer = data[index]; const want_read_count: DWORD = @min(std.math.maxInt(DWORD), buffer.len); @@ -6519,6 +6521,7 @@ fn fileReadPositionalPosix(userdata: ?*anyopaque, file: File, data: []const []u8 i += 1; } } + if (i == 0) return 0; const dest = iovecs_buffer[0..i]; assert(dest[0].len > 0); @@ -6614,6 +6617,7 @@ fn fileReadPositionalWindows(userdata: ?*anyopaque, file: File, data: []const [] var index: usize = 0; while (data[index].len == 0) index += 1; + if (index == 0) return 0; const buffer = data[index]; const want_read_count: DWORD = @min(std.math.maxInt(DWORD), buffer.len);