| ... | @@ -913,6 +913,7 @@ test { | ... | @@ -913,6 +913,7 @@ test { |
| 913 | } | 913 | } |
| 914 | | 914 | |
| 915 | const Io = @This(); | 915 | const Io = @This(); |
| | 916 | const fs = std.fs; |
| 916 | | 917 | |
| 917 | pub const EventLoop = @import("Io/EventLoop.zig"); | 918 | pub const EventLoop = @import("Io/EventLoop.zig"); |
| 918 | | 919 | |
| ... | @@ -946,6 +947,12 @@ pub const VTable = struct { | ... | @@ -946,6 +947,12 @@ pub const VTable = struct { |
| 946 | /// The length is equal to size in bytes of result type. | 947 | /// The length is equal to size in bytes of result type. |
| 947 | result: []u8, | 948 | result: []u8, |
| 948 | ) void, | 949 | ) void, |
| | 950 | |
| | 951 | createFile: *const fn (?*anyopaque, dir: fs.Dir, sub_path: []const u8, flags: fs.File.CreateFlags) fs.File.OpenError!fs.File, |
| | 952 | openFile: *const fn (?*anyopaque, dir: fs.Dir, sub_path: []const u8, flags: fs.File.OpenFlags) fs.File.OpenError!fs.File, |
| | 953 | closeFile: *const fn (?*anyopaque, fs.File) void, |
| | 954 | read: *const fn (?*anyopaque, file: fs.File, buffer: []u8) fs.File.ReadError!usize, |
| | 955 | write: *const fn (?*anyopaque, file: fs.File, buffer: []const u8) fs.File.WriteError!usize, |
| 949 | }; | 956 | }; |
| 950 | | 957 | |
| 951 | pub const AnyFuture = opaque {}; | 958 | pub const AnyFuture = opaque {}; |
| ... | @@ -991,3 +998,40 @@ pub fn async(io: Io, S: type, s: S) Future(@typeInfo(@TypeOf(S.start)).@"fn".ret | ... | @@ -991,3 +998,40 @@ pub fn async(io: Io, S: type, s: S) Future(@typeInfo(@TypeOf(S.start)).@"fn".ret |
| 991 | ); | 998 | ); |
| 992 | return future; | 999 | return future; |
| 993 | } | 1000 | } |
| | 1001 | |
| | 1002 | pub fn openFile(io: Io, dir: fs.Dir, sub_path: []const u8, flags: fs.File.OpenFlags) fs.File.OpenError!fs.File { |
| | 1003 | return io.vtable.openFile(io.userdata, dir, sub_path, flags); |
| | 1004 | } |
| | 1005 | |
| | 1006 | pub fn createFile(io: Io, dir: fs.Dir, sub_path: []const u8, flags: fs.File.CreateFlags) fs.File.OpenError!fs.File { |
| | 1007 | return io.vtable.createFile(io.userdata, dir, sub_path, flags); |
| | 1008 | } |
| | 1009 | |
| | 1010 | pub fn closeFile(io: Io, file: fs.File) void { |
| | 1011 | return io.vtable.closeFile(io.userdata, file); |
| | 1012 | } |
| | 1013 | |
| | 1014 | pub fn read(io: Io, file: fs.File, buffer: []u8) fs.File.ReadError!usize { |
| | 1015 | return io.vtable.read(io.userdata, file, buffer); |
| | 1016 | } |
| | 1017 | |
| | 1018 | pub fn write(io: Io, file: fs.File, buffer: []const u8) fs.File.WriteError!usize { |
| | 1019 | return io.vtable.write(io.userdata, file, buffer); |
| | 1020 | } |
| | 1021 | |
| | 1022 | pub fn writeAll(io: Io, file: fs.File, bytes: []const u8) fs.File.WriteError!void { |
| | 1023 | var index: usize = 0; |
| | 1024 | while (index < bytes.len) { |
| | 1025 | index += try io.write(file, bytes[index..]); |
| | 1026 | } |
| | 1027 | } |
| | 1028 | |
| | 1029 | pub fn readAll(io: Io, file: fs.File, buffer: []u8) fs.File.ReadError!usize { |
| | 1030 | var index: usize = 0; |
| | 1031 | while (index != buffer.len) { |
| | 1032 | const amt = try io.read(file, buffer[index..]); |
| | 1033 | if (amt == 0) break; |
| | 1034 | index += amt; |
| | 1035 | } |
| | 1036 | return index; |
| | 1037 | } |