@asyncCall

@asyncCall

@asyncCall(frame_buffer: []align(@alignOf(@Frame(anyAsyncFunction))) u8, result_ptr, function_ptr, args: anytype) anyframe->T

@asyncCall performs an async call on a function pointer, which may or may not be an async function.

The provided frame_buffer must be large enough to fit the entire function frame. This size can be determined with @frameSize. To provide a too-small buffer invokes safety-checked Undefined Behavior.

result_ptr is optional (null may be provided). If provided, the function call will write its result directly to the result pointer, which will be available to read after await completes. Any result location provided to await will copy the result from result_ptr.

test.zig

const std = @import("std");
const expect = std.testing.expect;

test "async fn pointer in a struct field" {
    var data: i32 = 1;
    const Foo = struct {
        bar: fn (*i32) callconv(.Async) void,
    };
    var foo = Foo{ .bar = func };
    var bytes: [64]u8 align(@alignOf(@Frame(func))) = undefined;
    const f = @asyncCall(&bytes, {}, foo.bar, .{&data});
    expect(data == 2);
    resume f;
    expect(data == 4);
}

fn func(y: *i32) void {
    defer y.* += 2;
    y.* += 1;
    suspend;
}
``` ```
$ zig test test.zig
1/1 test "async fn pointer in a struct field"... OK
All 1 tests passed.