From f27bd87ade2e43dce66963f43ce678e361ddbfc2 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Thu, 18 Dec 2025 23:05:51 -0800 Subject: [PATCH] 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. --- lib/std/Io/File.zig | 4 +--- lib/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 index e06f2ba40d..601a882605 100644 --- 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 index ce49049f02..ffa292a040 100644 --- 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);