| ... | ... | @@ -555,7 +555,6 @@ test { |
| 555 | 555 | } |
| 556 | 556 | |
| 557 | 557 | const Io = @This(); |
| 558 | | const fs = std.fs; |
| 559 | 558 | |
| 560 | 559 | pub const EventLoop = @import("Io/EventLoop.zig"); |
| 561 | 560 | |
| ... | ... | @@ -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: |
| 617 | | /// ``` |
| 618 | | /// struct { |
| 619 | | /// pub fn start(s: S) Result { ... } |
| 620 | | /// } |
| 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.?; |
| 615 | /// Calls `function` with `args`, such that the return value of the function is |
| 616 | /// not guaranteed to be available until `await` is called. |
| 617 | pub fn async(io: Io, function: anytype, args: anytype) Future(@typeInfo(@TypeOf(function)).@"fn".return_type.?) { |
| 618 | const Result = @typeInfo(@TypeOf(function)).@"fn".return_type.?; |
| 619 | const Args = @TypeOf(args); |
| 625 | 620 | const TypeErased = struct { |
| 626 | 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 | 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 | 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 | 629 | io.userdata, |
| 635 | 630 | @ptrCast((&future.result)[0..1]), |
| 636 | 631 | .fromByteUnits(@alignOf(Result)), |
| 637 | | if (@sizeOf(S) == 0) &.{} else @ptrCast((&s)[0..1]), // work around compiler bug |
| 638 | | .fromByteUnits(@alignOf(S)), |
| 632 | if (@sizeOf(Args) == 0) &.{} else @ptrCast((&args)[0..1]), // work around compiler bug |
| 633 | .fromByteUnits(@alignOf(Args)), |
| 639 | 634 | TypeErased.start, |
| 640 | 635 | ); |
| 641 | 636 | return future; |