zigar.thread.WorkQueue(ns).Asyncified(T) - chung-leong/zigar GitHub Wiki

Return a function type that has same arguments as the given function type with the addition of either a Promise or a Generator depending on whether the original function returns an iterator.

You generally don't need to use this function directly.

Usage

const std = @import("std");

const zigar = @import("zigar");

pub fn printTypeNames() void {
    const T1 = zigar.thread.WorkQueue(ns).Asyncified(@TypeOf(ns.hello));
    const T2 = zigar.thread.WorkQueue(ns).Asyncified(@TypeOf(ns.sequence));
    std.debug.print("{s}\n", .{@typeName(T1)});
    std.debug.print("{s}\n", .{@typeName(T2)});
}

const ns = struct {
    const Iterator = struct {
        current: usize = 0,
        total: usize,

        pub fn next(self: *@This()) ?usize {
            if (self.current >= self.total) return null;
            std.Thread.sleep(100 * 1000000); // pause for 100us
            self.current += 1;
            return self.current;
        }
    };

    pub fn hello() void {
        std.Thread.sleep(500 * 1000000); // pause for half a second
        std.debug.print("Hello, world!", .{});
    }

    pub fn sequence(len: usize) Iterator {
        return .{ .total = len };
    }
};
import { printTypeNames } from './work-queue-example-9.zig';

printTypeNames();
fn (types.Promise(void)) error{Unexpected,OutOfMemory}!void
fn (usize, types.Generator__struct_40865) error{Unexpected,OutOfMemory}!void

Arguments:

  • T: type
    Must be a function type.

Return value:

type


WorkQueue(ns)