authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-06-16 19:27:49-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-06-16 19:27:49-07:00
log20e20f9238b1f4537a8186d80afbdf7516e127f6
treebaabc53ee48aafa521542c2d3608c617c1fbac76
parent17f14e1d65b59ebb1a8b5b617cc31fb2614f0c6a

std.Progress.Node: add `none` init value


1 files changed, 11 insertions(+), 9 deletions(-)

lib/std/Progress.zig+11-9
...@@ -80,6 +80,8 @@ pub const Options = struct {...@@ -80,6 +80,8 @@ pub const Options = struct {
80pub const Node = struct {80pub const Node = struct {
81 index: OptionalIndex,81 index: OptionalIndex,
8282
83 pub const none: Node = .{ .index = .none };
84
83 pub const max_name_len = 40;85 pub const max_name_len = 40;
8486
85 const Storage = extern struct {87 const Storage = extern struct {
...@@ -177,9 +179,9 @@ pub const Node = struct {...@@ -177,9 +179,9 @@ pub const Node = struct {
177 pub fn start(node: Node, name: []const u8, estimated_total_items: usize) Node {179 pub fn start(node: Node, name: []const u8, estimated_total_items: usize) Node {
178 if (noop_impl) {180 if (noop_impl) {
179 assert(node.index == .none);181 assert(node.index == .none);
180 return .{ .index = .none };182 return Node.none;
181 }183 }
182 const node_index = node.index.unwrap() orelse return .{ .index = .none };184 const node_index = node.index.unwrap() orelse return Node.none;
183 const parent = node_index.toParent();185 const parent = node_index.toParent();
184186
185 const freelist_head = &global_progress.node_freelist_first;187 const freelist_head = &global_progress.node_freelist_first;
...@@ -196,7 +198,7 @@ pub const Node = struct {...@@ -196,7 +198,7 @@ pub const Node = struct {
196 if (free_index >= global_progress.node_storage.len) {198 if (free_index >= global_progress.node_storage.len) {
197 // Ran out of node storage memory. Progress for this node will not be tracked.199 // Ran out of node storage memory. Progress for this node will not be tracked.
198 _ = @atomicRmw(u32, &global_progress.node_end_index, .Sub, 1, .monotonic);200 _ = @atomicRmw(u32, &global_progress.node_end_index, .Sub, 1, .monotonic);
199 return .{ .index = .none };201 return Node.none;
200 }202 }
201203
202 return init(@enumFromInt(free_index), parent, name, estimated_total_items);204 return init(@enumFromInt(free_index), parent, name, estimated_total_items);
...@@ -357,7 +359,7 @@ pub fn start(options: Options) Node {...@@ -357,7 +359,7 @@ pub fn start(options: Options) Node {
357 global_progress.initial_delay_ns = options.initial_delay_ns;359 global_progress.initial_delay_ns = options.initial_delay_ns;
358360
359 if (noop_impl)361 if (noop_impl)
360 return .{ .index = .none };362 return Node.none;
361363
362 if (std.process.parseEnvVarInt("ZIG_PROGRESS", u31, 10)) |ipc_fd| {364 if (std.process.parseEnvVarInt("ZIG_PROGRESS", u31, 10)) |ipc_fd| {
363 global_progress.update_thread = std.Thread.spawn(.{}, ipcThreadRun, .{365 global_progress.update_thread = std.Thread.spawn(.{}, ipcThreadRun, .{
...@@ -368,12 +370,12 @@ pub fn start(options: Options) Node {...@@ -368,12 +370,12 @@ pub fn start(options: Options) Node {
368 }),370 }),
369 }) catch |err| {371 }) catch |err| {
370 std.log.warn("failed to spawn IPC thread for communicating progress to parent: {s}", .{@errorName(err)});372 std.log.warn("failed to spawn IPC thread for communicating progress to parent: {s}", .{@errorName(err)});
371 return .{ .index = .none };373 return Node.none;
372 };374 };
373 } else |env_err| switch (env_err) {375 } else |env_err| switch (env_err) {
374 error.EnvironmentVariableNotFound => {376 error.EnvironmentVariableNotFound => {
375 if (options.disable_printing) {377 if (options.disable_printing) {
376 return .{ .index = .none };378 return Node.none;
377 }379 }
378 const stderr = std.io.getStdErr();380 const stderr = std.io.getStdErr();
379 global_progress.terminal = stderr;381 global_progress.terminal = stderr;
...@@ -386,7 +388,7 @@ pub fn start(options: Options) Node {...@@ -386,7 +388,7 @@ pub fn start(options: Options) Node {
386 }388 }
387389
388 if (global_progress.terminal_mode == .off) {390 if (global_progress.terminal_mode == .off) {
389 return .{ .index = .none };391 return Node.none;
390 }392 }
391393
392 if (have_sigwinch) {394 if (have_sigwinch) {
...@@ -408,12 +410,12 @@ pub fn start(options: Options) Node {...@@ -408,12 +410,12 @@ pub fn start(options: Options) Node {
408 global_progress.update_thread = thread;410 global_progress.update_thread = thread;
409 } else |err| {411 } else |err| {
410 std.log.warn("unable to spawn thread for printing progress to terminal: {s}", .{@errorName(err)});412 std.log.warn("unable to spawn thread for printing progress to terminal: {s}", .{@errorName(err)});
411 return .{ .index = .none };413 return Node.none;
412 }414 }
413 },415 },
414 else => |e| {416 else => |e| {
415 std.log.warn("invalid ZIG_PROGRESS file descriptor integer: {s}", .{@errorName(e)});417 std.log.warn("invalid ZIG_PROGRESS file descriptor integer: {s}", .{@errorName(e)});
416 return .{ .index = .none };418 return Node.none;
417 },419 },
418 }420 }
419421