zigar.function.GeneratorOf(function) - chung-leong/zigar GitHub Wiki

Define a generator type compatible with function, whose return value must be a iterator (or an error union of one). If the iterator's next() method accepts an allocator, the generator will have an allocator attached.

Example:

const std = @import("std");
const zigar = @import("zigar");

fn test1() error{InitFailed}!Iterator1 {
    return error.InitFailed;
}

const Iterator1 = struct {
    pub fn next(_: *@This()) error{NextFailed}!?i32 {
        return null;
    }
};

fn test2() error{InitFailed}!Iterator2 {
    return error.InitFailed;
}

const Iterator1 = struct {
    pub fn next(_: *@This(), _: std.mem.Allocator) error{NextFailed}!?i32 {
        return null;
    }
};

comptime {
    std.debug.assert(zigar.function.GeneratorOf(test1) == zigar.function.Generator(error{ InitFailed, NextFailed }!?i32), false);
    std.debug.assert(zigar.function.GeneratorOf(test2) == zigar.function.Generator(error{ InitFailed, NextFailed }!?i32), true);
}

Arguments:

  • function: anytype
    A function or a function type.

Return value:

Generator(T, allocating)


Generator(T, allocating) | GeneratorArgOf(function)