Skip to content

Commit

Permalink
Problem: what's the point of generators?
Browse files Browse the repository at this point in the history
Generators look nice, but they are still slower than callbacks
(as they naturally have more harness code).

Solution: provide Map and Join generators

The beauty of generators is that they are async and this allows us
to build higher-level primitives.

Of a particular interest is Join that will allow to consume values
from two generators, as they come.
  • Loading branch information
yrashk committed Dec 29, 2021
1 parent 4bcbdb1 commit bda35aa
Show file tree
Hide file tree
Showing 8 changed files with 723 additions and 444 deletions.
7 changes: 5 additions & 2 deletions benchmarks.zig
@@ -1,6 +1,9 @@
const std = @import("std");
const Handle = @import("./src/lib.zig").Handle;
const Generator = @import("./src/lib.zig").Generator;

const generator = @import("generator");
const Handle = generator.Handle;
const Generator = generator.Generator;
const Map = generator.Map;

pub const io_mode = .evented;

Expand Down
2 changes: 1 addition & 1 deletion build.zig
Expand Up @@ -10,7 +10,7 @@ pub fn build(b: *std.build.Builder) void {
lib.setBuildMode(mode);
lib.install();

const tests = b.addTest("src/lib.zig");
const tests = b.addTest("src/tests.zig");
tests.test_evented_io = true;
tests.setBuildMode(mode);

Expand Down
37 changes: 37 additions & 0 deletions src/_.zig
@@ -0,0 +1,37 @@
const std = @import("std");

/// Internal use:
///
/// Finds and extracts any function in a given struct `T`
/// that has a type `Sig`
pub fn extractFn(comptime T: type, comptime Sig: type) Sig {
switch (@typeInfo(T)) {
.Struct => {
const decls = std.meta.declarations(T);
inline for (decls) |decl| {
if (decl.is_pub) {
switch (decl.data) {
.Fn => |fn_decl| {
if (fn_decl.fn_type == Sig) {
return @field(T, decl.name);
}
},
else => {},
}
}
}
@compileError("no public functions found");
},
else => @compileError("only structs are allowed"),
}
}

test "extractFn" {
const f = extractFn(struct {
pub fn check(_: u8, _: u8) void {}
pub fn add(a: u8, b: u8) u8 {
return a + b;
}
}, fn (u8, u8) u8);
try std.testing.expect(f(1, 2) == 3);
}

0 comments on commit bda35aa

Please sign in to comment.