libuv: stream try_write

This commit is contained in:
Mitchell Hashimoto
2022-04-25 19:02:31 -07:00
parent a0424d3a86
commit 0258b24d1f
2 changed files with 19 additions and 0 deletions

View File

@ -127,6 +127,13 @@ test "Pipe" {
// Check our data
try testing.expectEqual(@as(usize, 5), data.data.items.len);
try testing.expectEqualStrings("hello", data.data.items);
data.data.clearRetainingCapacity();
// Try writing directly
_ = try writer.tryWrite(&[_][]const u8{"world"});
_ = try loop.run(.once);
try testing.expectEqual(@as(usize, 5), data.data.items.len);
try testing.expectEqualStrings("world", data.data.items);
// End
reader.readStop();

View File

@ -63,6 +63,18 @@ pub fn Stream(comptime T: type) type {
));
}
/// Same as uv_write(), but wont queue a write request if it cant
/// be completed immediately.
pub fn tryWrite(self: T, bufs: []const []const u8) !usize {
const res = c.uv_try_write(
@ptrCast(*c.uv_stream_t, self.handle),
@ptrCast([*c]const c.uv_buf_t, bufs.ptr),
@intCast(c_uint, bufs.len),
);
try errors.convertError(res);
return @intCast(usize, res);
}
/// Read data from an incoming stream. The uv_read_cb callback will
/// be made several times until there is no more data to read or
/// uv_read_stop() is called.