| ... | @@ -555,6 +555,7 @@ test { | ... | @@ -555,6 +555,7 @@ test { |
| 555 | } | 555 | } |
| 556 | | 556 | |
| 557 | const Io = @This(); | 557 | const Io = @This(); |
| | 558 | const fs = std.fs; |
| 558 | | 559 | |
| 559 | pub const EventLoop = @import("Io/EventLoop.zig"); | 560 | pub const EventLoop = @import("Io/EventLoop.zig"); |
| 560 | | 561 | |
| ... | @@ -588,6 +589,12 @@ pub const VTable = struct { | ... | @@ -588,6 +589,12 @@ pub const VTable = struct { |
| 588 | /// The length is equal to size in bytes of result type. | 589 | /// The length is equal to size in bytes of result type. |
| 589 | result: []u8, | 590 | result: []u8, |
| 590 | ) void, | 591 | ) void, |
| | 592 | |
| | 593 | createFile: *const fn (?*anyopaque, dir: fs.Dir, sub_path: []const u8, flags: fs.File.CreateFlags) fs.File.OpenError!fs.File, |
| | 594 | openFile: *const fn (?*anyopaque, dir: fs.Dir, sub_path: []const u8, flags: fs.File.OpenFlags) fs.File.OpenError!fs.File, |
| | 595 | closeFile: *const fn (?*anyopaque, fs.File) void, |
| | 596 | read: *const fn (?*anyopaque, file: fs.File, buffer: []u8) fs.File.ReadError!usize, |
| | 597 | write: *const fn (?*anyopaque, file: fs.File, buffer: []const u8) fs.File.WriteError!usize, |
| 591 | }; | 598 | }; |
| 592 | | 599 | |
| 593 | pub const AnyFuture = opaque {}; | 600 | pub const AnyFuture = opaque {}; |
| ... | @@ -633,3 +640,40 @@ pub fn async(io: Io, S: type, s: S) Future(@typeInfo(@TypeOf(S.start)).@"fn".ret | ... | @@ -633,3 +640,40 @@ pub fn async(io: Io, S: type, s: S) Future(@typeInfo(@TypeOf(S.start)).@"fn".ret |
| 633 | ); | 640 | ); |
| 634 | return future; | 641 | return future; |
| 635 | } | 642 | } |
| | 643 | |
| | 644 | pub fn openFile(io: Io, dir: fs.Dir, sub_path: []const u8, flags: fs.File.OpenFlags) fs.File.OpenError!fs.File { |
| | 645 | return io.vtable.openFile(io.userdata, dir, sub_path, flags); |
| | 646 | } |
| | 647 | |
| | 648 | pub fn createFile(io: Io, dir: fs.Dir, sub_path: []const u8, flags: fs.File.CreateFlags) fs.File.OpenError!fs.File { |
| | 649 | return io.vtable.createFile(io.userdata, dir, sub_path, flags); |
| | 650 | } |
| | 651 | |
| | 652 | pub fn closeFile(io: Io, file: fs.File) void { |
| | 653 | return io.vtable.closeFile(io.userdata, file); |
| | 654 | } |
| | 655 | |
| | 656 | pub fn read(io: Io, file: fs.File, buffer: []u8) fs.File.ReadError!usize { |
| | 657 | return io.vtable.read(io.userdata, file, buffer); |
| | 658 | } |
| | 659 | |
| | 660 | pub fn write(io: Io, file: fs.File, buffer: []const u8) fs.File.WriteError!usize { |
| | 661 | return io.vtable.write(io.userdata, file, buffer); |
| | 662 | } |
| | 663 | |
| | 664 | pub fn writeAll(io: Io, file: fs.File, bytes: []const u8) fs.File.WriteError!void { |
| | 665 | var index: usize = 0; |
| | 666 | while (index < bytes.len) { |
| | 667 | index += try io.write(file, bytes[index..]); |
| | 668 | } |
| | 669 | } |
| | 670 | |
| | 671 | pub fn readAll(io: Io, file: fs.File, buffer: []u8) fs.File.ReadError!usize { |
| | 672 | var index: usize = 0; |
| | 673 | while (index != buffer.len) { |
| | 674 | const amt = try io.read(file, buffer[index..]); |
| | 675 | if (amt == 0) break; |
| | 676 | index += amt; |
| | 677 | } |
| | 678 | return index; |
| | 679 | } |