Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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
- Host: GitHub
- URL: https://github.com/yrashk/zig-generator
- Owner: yrashk
- License: mit
- Created: 2021-12-26T19:22:47.000Z (about 3 years ago)
- Default Branch: master
- Last Pushed: 2022-01-06T05:17:32.000Z (about 3 years ago)
- Last Synced: 2024-08-03T04:09:02.407Z (7 months ago)
- Language: Zig
- Size: 1.57 MB
- Stars: 38
- Watchers: 3
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-zig - zig-generator🗒️Async generator type for Zig
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);
}
```