Commit a29b2122d2

Andrew Kelley <andrew@ziglang.org>
2025-03-29 23:22:00
better API for Io.async
1 parent d958077
Changed files (1)
lib
std
lib/std/Io.zig
@@ -555,7 +555,6 @@ test {
 }
 
 const Io = @This();
-const fs = std.fs;
 
 pub const EventLoop = @import("Io/EventLoop.zig");
 
@@ -613,20 +612,16 @@ pub fn Future(Result: type) type {
     };
 }
 
-/// `s` is a struct instance that contains a function like this:
-/// ```
-/// struct {
-///     pub fn start(s: S) Result { ... }
-/// }
-/// ```
-/// where `Result` is any type.
-pub fn async(io: Io, S: type, s: S) Future(@typeInfo(@TypeOf(S.start)).@"fn".return_type.?) {
-    const Result = @typeInfo(@TypeOf(S.start)).@"fn".return_type.?;
+/// Calls `function` with `args`, such that the return value of the function is
+/// not guaranteed to be available until `await` is called.
+pub fn async(io: Io, function: anytype, args: anytype) Future(@typeInfo(@TypeOf(function)).@"fn".return_type.?) {
+    const Result = @typeInfo(@TypeOf(function)).@"fn".return_type.?;
+    const Args = @TypeOf(args);
     const TypeErased = struct {
         fn start(context: *const anyopaque, result: *anyopaque) void {
-            const context_casted: *const S = @alignCast(@ptrCast(context));
+            const args_casted: *const Args = @alignCast(@ptrCast(context));
             const result_casted: *Result = @ptrCast(@alignCast(result));
-            result_casted.* = S.start(context_casted.*);
+            result_casted.* = @call(.auto, function, args_casted.*);
         }
     };
     var future: Future(Result) = undefined;
@@ -634,8 +629,8 @@ pub fn async(io: Io, S: type, s: S) Future(@typeInfo(@TypeOf(S.start)).@"fn".ret
         io.userdata,
         @ptrCast((&future.result)[0..1]),
         .fromByteUnits(@alignOf(Result)),
-        if (@sizeOf(S) == 0) &.{} else @ptrCast((&s)[0..1]), // work around compiler bug
-        .fromByteUnits(@alignOf(S)),
+        if (@sizeOf(Args) == 0) &.{} else @ptrCast((&args)[0..1]), // work around compiler bug
+        .fromByteUnits(@alignOf(Args)),
         TypeErased.start,
     );
     return future;