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 " ++
std.fmt.comptimePrint("{d}", .{captures_size}) ++ " bytes");
const captures_align = @alignOf(Captures);
var function = Self{
.context = undefined,
.applyErased = struct {
fn do(erased: *anyopaque, a: A, b: B) Out {
return call(@ptrCast(*Captures, @alignCast(
captures_align, erased)).*, a, b);
fn applyErased(erased: *anyopaque, a: A, b: B) Out {
return call(if (Captures == void) {} else @ptrCast(*Captures,
@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;
}
@ -108,11 +108,11 @@ pub fn Function(comptime captures_size: usize, comptime In: type, comptime Out:
.context = undefined,
.applyErased = struct {
fn do(erased: *anyopaque, input: In) Out {
return call(@ptrCast(*Captures, @alignCast(
captures_align, erased)).*, input);
fn applyErased(erased: *anyopaque, input: In) Out {
return call(if (Captures == void) {} else @ptrCast(*Captures,
@alignCast(@alignOf(Captures), erased)).*, input);
}
}.do,
}.applyErased,
};
@ptrCast(*Captures, @alignCast(captures_align, &function.context)).* = captures;