From 3881f9053ed8e594ff28e7ec94c72ab3d3866303 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 26 Apr 2022 16:18:21 -0700 Subject: [PATCH] libuv: expose WriteReq --- src/libuv/main.zig | 5 ++++- src/libuv/stream.zig | 12 ++++++++---- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/libuv/main.zig b/src/libuv/main.zig index 40fa90ed5..44e303fb9 100644 --- a/src/libuv/main.zig +++ b/src/libuv/main.zig @@ -1,3 +1,5 @@ +const stream = @import("stream.zig"); + pub const Loop = @import("Loop.zig"); pub const Async = @import("Async.zig"); pub const Pipe = @import("Pipe.zig"); @@ -6,12 +8,13 @@ pub const Tty = @import("Tty.zig"); pub const Sem = @import("Sem.zig"); pub const Thread = @import("Thread.zig"); pub const Error = @import("error.zig").Error; +pub const WriteReq = stream.WriteReq; pub const Embed = @import("Embed.zig"); test { - _ = @import("stream.zig"); _ = @import("tests.zig"); + _ = stream; _ = Loop; _ = Async; diff --git a/src/libuv/stream.zig b/src/libuv/stream.zig index 832f9edec..482351e63 100644 --- a/src/libuv/stream.zig +++ b/src/libuv/stream.zig @@ -142,7 +142,11 @@ pub fn Stream(comptime T: type) type { /// behaviour. It is safe to reuse the uv_write_t object only after the /// callback passed to uv_write is fired. pub const WriteReq = struct { - req: *c.uv_write_t, + /// This is the underlying type that WriteReq wraps. This is exposed + /// so that you can pre-allocate the type and wrap it in a WrapReq. + pub const T = c.uv_write_t; + + req: *T, pub fn init(alloc: Allocator) !WriteReq { var req = try alloc.create(c.uv_write_t); @@ -157,12 +161,12 @@ pub const WriteReq = struct { /// Pointer to the stream where this write request is running. /// T should be a high-level handle type such as "Pipe". - pub fn handle(self: WriteReq, comptime T: type) ?T { - const tInfo = @typeInfo(T).Struct; + pub fn handle(self: WriteReq, comptime HT: type) ?HT { + const tInfo = @typeInfo(HT).Struct; const HandleType = tInfo.fields[0].field_type; return if (self.req.handle) |ptr| - return T{ .handle = @ptrCast(HandleType, ptr) } + return HT{ .handle = @ptrCast(HandleType, ptr) } else null; }