Application Context Implementation #4

Closed
kayomn wants to merge 93 commits from event-loop-dev into main
1 changed files with 11 additions and 11 deletions
Showing only changes of commit 989de70461 - Show all commits

View File

@ -48,20 +48,20 @@ pub fn BiFunction(comptime captures_size: usize, comptime A: type,
@compileError("`captures` must be smaller than or equal to " ++ @compileError("`captures` must be smaller than or equal to " ++
std.fmt.comptimePrint("{d}", .{captures_size}) ++ " bytes"); std.fmt.comptimePrint("{d}", .{captures_size}) ++ " bytes");
const captures_align = @alignOf(Captures);
var function = Self{ var function = Self{
.context = undefined, .context = undefined,
.applyErased = struct { .applyErased = struct {
fn do(erased: *anyopaque, a: A, b: B) Out { fn applyErased(erased: *anyopaque, a: A, b: B) Out {
return call(@ptrCast(*Captures, @alignCast( return call(if (Captures == void) {} else @ptrCast(*Captures,
captures_align, erased)).*, a, b); @alignCast(@alignOf(Captures), erased)).*, a, b);
} }
}.do, }.applyErased,
}; };
@ptrCast(*Captures, @alignCast(captures_align, &function.context)).* = captures; if (captures != {}) {
@ptrCast(*Captures, @alignCast(@alignOf(Captures), &function.context)).* = captures;
}
return function; return function;
} }
@ -108,11 +108,11 @@ pub fn Function(comptime captures_size: usize, comptime In: type, comptime Out:
.context = undefined, .context = undefined,
.applyErased = struct { .applyErased = struct {
fn do(erased: *anyopaque, input: In) Out { fn applyErased(erased: *anyopaque, input: In) Out {
return call(@ptrCast(*Captures, @alignCast( return call(if (Captures == void) {} else @ptrCast(*Captures,
captures_align, erased)).*, input); @alignCast(@alignOf(Captures), erased)).*, input);
} }
}.do, }.applyErased,
}; };
@ptrCast(*Captures, @alignCast(captures_align, &function.context)).* = captures; @ptrCast(*Captures, @alignCast(captures_align, &function.context)).* = captures;