zigar.function.Generator(T).end(self) - chung-leong/zigar GitHub Wiki

Indicate the end of content generation by calling yield() with null.

Usage:

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

const Generator = zigar.function.Generator(?error{OutOfMemory}![]u8);

pub fn start() !void {
    try zigar.thread.use();
}

pub fn stop() void {
    zigar.thread.end();
}

pub fn getStrings(a: std.mem.Allocator, generator: Generator) !void {
    const thread = try std.Thread.spawn(.{}, generateStrings, .{ a, generator });
    thread.detach();
}

fn generateStrings(a: std.mem.Allocator, generator: Generator) void {
    for (0..10) |i| {
        if (!generator.yield(std.fmt.allocPrint(a, "string {d}", .{i}))) break;
    } else generator.end();
}
import { getStrings, start, stop } from './generator-example-1.zig';

try {
    start();
    for await (const s of getStrings()) {
        console.log(s.string);
        await new Promise(r => setTimeout(r, 100));
    }
} finally {
    stop();
}

Arguments:

  • self: @This()

Return value:

void


Generator(T)