Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/yrashk/zig-generator

Async generator type for Zig
https://github.com/yrashk/zig-generator

Last synced: 3 months ago
JSON representation

Async generator type for Zig

Awesome Lists containing this project

README

        

# Zig Generator

This library provides a way to write generators in Zig.

Features:

* Supports async generator functions
* Propagates generator errors
* Return value capture

## Usage

Here's an example of its basic usage:

```zig
const std = @import("std");
const gen = @import("generator");

const Ty = struct {
pub fn generate(_: *@This(), handle: *gen.Handle(u8)) !u8 {
try handle.yield(0);
try handle.yield(1);
try handle.yield(2);
return 3;
}
};

const G = gen.Generator(Ty, u8);

pub const io_mode = .evented;

pub fn main() !void {
var g = G.init(Ty{});

std.debug.assert((try g.next()).? == 0);
std.debug.assert((try g.next()).? == 1);
std.debug.assert((try g.next()).? == 2);
std.debug.assert((try g.next()) == null);
std.debug.assert(g.state.Returned == 3);
}
```