Skip to content

Commit 49f7b3c

Browse files
Cleanup some io code
1 parent 8617bc0 commit 49f7b3c

3 files changed

Lines changed: 12 additions & 17 deletions

File tree

examples/async_tasks.zig

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,17 +89,17 @@ const Model = struct {
8989
}
9090

9191
fn task1(io: std.Io) ?Msg {
92-
sleepNs(io, 500_000_000); // 500ms
92+
std.Io.sleep(io, .fromMilliseconds(500), .boot) catch unreachable; // 500ms
9393
return .{ .task_complete = .{ .id = 0, .value = "Task 1: computed pi = 3.14159" } };
9494
}
9595

9696
fn task2(io: std.Io) ?Msg {
97-
sleepNs(io, 1_000_000_000); // 1s
97+
std.Io.sleep(io, .fromMilliseconds(1000), .boot) catch unreachable; // 1s
9898
return .{ .task_complete = .{ .id = 1, .value = "Task 2: fetched 42 records" } };
9999
}
100100

101101
fn task3(io: std.Io) ?Msg {
102-
sleepNs(io, 750_000_000); // 750ms
102+
std.Io.sleep(io, .fromMilliseconds(750), .boot) catch unreachable; // 750ms
103103
return .{ .task_complete = .{ .id = 2, .value = "Task 3: file processed OK" } };
104104
}
105105

@@ -121,7 +121,8 @@ const Model = struct {
121121
box_s = box_s.paddingAll(1);
122122
box_s = box_s.width(45);
123123

124-
const results_text = std.fmt.allocPrint(alloc,
124+
const results_text = std.fmt.allocPrint(
125+
alloc,
125126
"1: {s}\n2: {s}\n3: {s}",
126127
.{ self.results[0], self.results[1], self.results[2] },
127128
) catch "";
@@ -130,7 +131,8 @@ const Model = struct {
130131
help_s = help_s.fg(zz.Color.gray(10));
131132
help_s = help_s.inline_style(true);
132133

133-
const content = std.fmt.allocPrint(alloc,
134+
const content = std.fmt.allocPrint(
135+
alloc,
134136
"{s}\n\n{s}\n\n{s}\n\n{s}",
135137
.{
136138
title_s.render(alloc, "Async Tasks Demo") catch "Async Tasks",

examples/file_browser.zig

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,10 @@ const Model = struct {
6868

6969
// Read first 500 bytes
7070
var buf: [500]u8 = undefined;
71-
const bytes_read = file.readStreaming(io, &.{&buf}) catch |err| switch (err) {
72-
error.EndOfStream => 0,
73-
else => {
74-
self.error_message = "Cannot read file";
75-
self.preview.clearRetainingCapacity();
76-
return;
77-
},
71+
const bytes_read = file.readPositionalAll(io, &buf, 0) catch {
72+
self.error_message = "Cannot read file";
73+
self.preview.clearRetainingCapacity();
74+
return;
7875
};
7976

8077
self.preview.clearRetainingCapacity();

src/core/program.zig

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -763,11 +763,7 @@ pub fn Program(comptime Model: type) type {
763763

764764
fn sleepNs(io: std.Io, nanoseconds: u64) void {
765765
if (nanoseconds == 0) return;
766-
const duration: std.Io.Clock.Duration = .{
767-
.raw = std.Io.Duration.fromNanoseconds(@intCast(nanoseconds)),
768-
.clock = .boot,
769-
};
770-
duration.sleep(io) catch {};
766+
std.Io.sleep(io, .fromNanoseconds(nanoseconds), .boot) catch unreachable;
771767
}
772768

773769
fn resetFrameAllocator(self: *Self) void {

0 commit comments

Comments
 (0)