| ... | ... | @@ -913,7 +913,6 @@ test { |
| 913 | 913 | } |
| 914 | 914 | |
| 915 | 915 | const Io = @This(); |
| 916 | | const fs = std.fs; |
| 917 | 916 | |
| 918 | 917 | pub const EventLoop = @import("Io/EventLoop.zig"); |
| 919 | 918 | |
| ... | ... | @@ -971,20 +970,16 @@ pub fn Future(Result: type) type { |
| 971 | 970 | }; |
| 972 | 971 | } |
| 973 | 972 | |
| 974 | | /// `s` is a struct instance that contains a function like this: |
| 975 | | /// ``` |
| 976 | | /// struct { |
| 977 | | /// pub fn start(s: S) Result { ... } |
| 978 | | /// } |
| 979 | | /// ``` |
| 980 | | /// where `Result` is any type. |
| 981 | | pub fn async(io: Io, S: type, s: S) Future(@typeInfo(@TypeOf(S.start)).@"fn".return_type.?) { |
| 982 | | const Result = @typeInfo(@TypeOf(S.start)).@"fn".return_type.?; |
| 973 | /// Calls `function` with `args`, such that the return value of the function is |
| 974 | /// not guaranteed to be available until `await` is called. |
| 975 | pub fn async(io: Io, function: anytype, args: anytype) Future(@typeInfo(@TypeOf(function)).@"fn".return_type.?) { |
| 976 | const Result = @typeInfo(@TypeOf(function)).@"fn".return_type.?; |
| 977 | const Args = @TypeOf(args); |
| 983 | 978 | const TypeErased = struct { |
| 984 | 979 | fn start(context: *const anyopaque, result: *anyopaque) void { |
| 985 | | const context_casted: *const S = @alignCast(@ptrCast(context)); |
| 980 | const args_casted: *const Args = @alignCast(@ptrCast(context)); |
| 986 | 981 | const result_casted: *Result = @ptrCast(@alignCast(result)); |
| 987 | | result_casted.* = S.start(context_casted.*); |
| 982 | result_casted.* = @call(.auto, function, args_casted.*); |
| 988 | 983 | } |
| 989 | 984 | }; |
| 990 | 985 | var future: Future(Result) = undefined; |
| ... | ... | @@ -992,8 +987,8 @@ pub fn async(io: Io, S: type, s: S) Future(@typeInfo(@TypeOf(S.start)).@"fn".ret |
| 992 | 987 | io.userdata, |
| 993 | 988 | @ptrCast((&future.result)[0..1]), |
| 994 | 989 | .fromByteUnits(@alignOf(Result)), |
| 995 | | if (@sizeOf(S) == 0) &.{} else @ptrCast((&s)[0..1]), // work around compiler bug |
| 996 | | .fromByteUnits(@alignOf(S)), |
| 990 | if (@sizeOf(Args) == 0) &.{} else @ptrCast((&args)[0..1]), // work around compiler bug |
| 991 | .fromByteUnits(@alignOf(Args)), |
| 997 | 992 | TypeErased.start, |
| 998 | 993 | ); |
| 999 | 994 | return future; |