| ... | @@ -555,7 +555,6 @@ test { | ... | @@ -555,7 +555,6 @@ test { |
| 555 | } | 555 | } |
| 556 | | 556 | |
| 557 | const Io = @This(); | 557 | const Io = @This(); |
| 558 | const fs = std.fs; | | |
| 559 | | 558 | |
| 560 | pub const EventLoop = @import("Io/EventLoop.zig"); | 559 | pub const EventLoop = @import("Io/EventLoop.zig"); |
| 561 | | 560 | |
| ... | @@ -613,20 +612,16 @@ pub fn Future(Result: type) type { | ... | @@ -613,20 +612,16 @@ pub fn Future(Result: type) type { |
| 613 | }; | 612 | }; |
| 614 | } | 613 | } |
| 615 | | 614 | |
| 616 | /// `s` is a struct instance that contains a function like this: | 615 | /// Calls `function` with `args`, such that the return value of the function is |
| 617 | /// ``` | 616 | /// not guaranteed to be available until `await` is called. |
| 618 | /// struct { | 617 | pub fn async(io: Io, function: anytype, args: anytype) Future(@typeInfo(@TypeOf(function)).@"fn".return_type.?) { |
| 619 | /// pub fn start(s: S) Result { ... } | 618 | const Result = @typeInfo(@TypeOf(function)).@"fn".return_type.?; |
| 620 | /// } | 619 | const Args = @TypeOf(args); |
| 621 | /// ``` | | |
| 622 | /// where `Result` is any type. | | |
| 623 | pub fn async(io: Io, S: type, s: S) Future(@typeInfo(@TypeOf(S.start)).@"fn".return_type.?) { | | |
| 624 | const Result = @typeInfo(@TypeOf(S.start)).@"fn".return_type.?; | | |
| 625 | const TypeErased = struct { | 620 | const TypeErased = struct { |
| 626 | fn start(context: *const anyopaque, result: *anyopaque) void { | 621 | fn start(context: *const anyopaque, result: *anyopaque) void { |
| 627 | const context_casted: *const S = @alignCast(@ptrCast(context)); | 622 | const args_casted: *const Args = @alignCast(@ptrCast(context)); |
| 628 | const result_casted: *Result = @ptrCast(@alignCast(result)); | 623 | const result_casted: *Result = @ptrCast(@alignCast(result)); |
| 629 | result_casted.* = S.start(context_casted.*); | 624 | result_casted.* = @call(.auto, function, args_casted.*); |
| 630 | } | 625 | } |
| 631 | }; | 626 | }; |
| 632 | var future: Future(Result) = undefined; | 627 | var future: Future(Result) = undefined; |
| ... | @@ -634,8 +629,8 @@ pub fn async(io: Io, S: type, s: S) Future(@typeInfo(@TypeOf(S.start)).@"fn".ret | ... | @@ -634,8 +629,8 @@ pub fn async(io: Io, S: type, s: S) Future(@typeInfo(@TypeOf(S.start)).@"fn".ret |
| 634 | io.userdata, | 629 | io.userdata, |
| 635 | @ptrCast((&future.result)[0..1]), | 630 | @ptrCast((&future.result)[0..1]), |
| 636 | .fromByteUnits(@alignOf(Result)), | 631 | .fromByteUnits(@alignOf(Result)), |
| 637 | if (@sizeOf(S) == 0) &.{} else @ptrCast((&s)[0..1]), // work around compiler bug | 632 | if (@sizeOf(Args) == 0) &.{} else @ptrCast((&args)[0..1]), // work around compiler bug |
| 638 | .fromByteUnits(@alignOf(S)), | 633 | .fromByteUnits(@alignOf(Args)), |
| 639 | TypeErased.start, | 634 | TypeErased.start, |
| 640 | ); | 635 | ); |
| 641 | return future; | 636 | return future; |