zigar.function.Generator(T, allocating).end(self) ᴾᴴᴾ - chung-leong/zigar GitHub Wiki

JavaScript | PHP


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, true);

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

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

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

fn generateStrings(generator: Generator) void {
    const a = generator.allocator;
    for (0..10) |i| {
        if (!generator.yield(std.fmt.allocPrint(a, "string {d}", .{i}))) break;
    } else generator.end();
}
<?php

$m = zigar_use(__DIR__ . '/generator-example-1.zig');

try {
    $m->start();
    foreach ($m->getStrings() as $s) {
        echo "$s\n";
    }
} finally {
    $m->stop();
}
string 0
string 1
string 2
string 3
string 4
string 5
string 6
string 7
string 8
string 9

Arguments:

  • self: @This()

Return value:

void


Generator(T, allocating)