authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2024-01-28 00:30:24+02:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2024-02-01 15:22:36+02:00
log220d3264c929187cb72111fcf75a223f9e4a7399
tree9eb71fb86133145d0b962074e13a00d043260bf1
parent776cd673f206099012d789fd5d05d49dd72b9faa

std: make options a struct instance instead of a namespace


10 files changed, 52 insertions(+), 113 deletions(-)

lib/std/log.zig+3-3
......@@ -18,12 +18,12 @@
1818//! ```
1919//! const std = @import("std");
2020//!
21//! pub const std_options = struct {
21//! pub const std_options = .{
2222//! // Set the log level to info
23//! pub const log_level = .info;
23//! .log_level = .info,
2424//!
2525//! // Define logFn to override the std implementation
26//! pub const logFn = myLogFn;
26//! .logFn = myLogFn,
2727//! };
2828//!
2929//! pub fn myLogFn(
lib/std/std.zig+21-78
......@@ -195,79 +195,35 @@ pub const zig = @import("zig.zig");
195195pub const start = @import("start.zig");
196196
197197const root = @import("root");
198const options_override = if (@hasDecl(root, "std_options")) root.std_options else struct {};
199198
200199/// Stdlib-wide options that can be overridden by the root file.
201pub const options = struct {
202 pub const enable_segfault_handler: bool = if (@hasDecl(options_override, "enable_segfault_handler"))
203 options_override.enable_segfault_handler
204 else
205 debug.default_enable_segfault_handler;
200pub const options: Options = if (@hasDecl(root, "std_options")) root.std_options else .{};
201
202pub const Options = struct {
203 enable_segfault_handler: bool = debug.default_enable_segfault_handler,
206204
207205 /// Function used to implement `std.fs.cwd` for WASI.
208 pub const wasiCwd: fn () fs.Dir = if (@hasDecl(options_override, "wasiCwd"))
209 options_override.wasiCwd
210 else
211 fs.defaultWasiCwd;
212
213 /// The application's chosen I/O mode.
214 pub const io_mode: io.Mode = if (@hasDecl(options_override, "io_mode"))
215 options_override.io_mode
216 else if (@hasDecl(options_override, "event_loop"))
217 .evented
218 else
219 .blocking;
220
221 pub const event_loop: event.Loop.Instance = if (@hasDecl(options_override, "event_loop"))
222 options_override.event_loop
223 else
224 event.Loop.default_instance;
225
226 pub const event_loop_mode: event.Loop.Mode = if (@hasDecl(options_override, "event_loop_mode"))
227 options_override.event_loop_mode
228 else
229 event.Loop.default_mode;
206 wasiCwd: fn () fs.Dir = fs.defaultWasiCwd,
230207
231208 /// The current log level.
232 pub const log_level: log.Level = if (@hasDecl(options_override, "log_level"))
233 options_override.log_level
234 else
235 log.default_level;
209 log_level: log.Level = log.default_level,
236210
237 pub const log_scope_levels: []const log.ScopeLevel = if (@hasDecl(options_override, "log_scope_levels"))
238 options_override.log_scope_levels
239 else
240 &.{};
211 log_scope_levels: []const log.ScopeLevel = &.{},
241212
242 pub const logFn: fn (
213 logFn: fn (
243214 comptime message_level: log.Level,
244215 comptime scope: @TypeOf(.enum_literal),
245216 comptime format: []const u8,
246217 args: anytype,
247 ) void = if (@hasDecl(options_override, "logFn"))
248 options_override.logFn
249 else
250 log.defaultLog;
251
252 pub const fmt_max_depth = if (@hasDecl(options_override, "fmt_max_depth"))
253 options_override.fmt_max_depth
254 else
255 fmt.default_max_depth;
256
257 pub const cryptoRandomSeed: fn (buffer: []u8) void = if (@hasDecl(options_override, "cryptoRandomSeed"))
258 options_override.cryptoRandomSeed
259 else
260 @import("crypto/tlcsprng.zig").defaultRandomSeed;
261
262 pub const crypto_always_getrandom: bool = if (@hasDecl(options_override, "crypto_always_getrandom"))
263 options_override.crypto_always_getrandom
264 else
265 false;
266
267 pub const crypto_fork_safety: bool = if (@hasDecl(options_override, "crypto_fork_safety"))
268 options_override.crypto_fork_safety
269 else
270 true;
218 ) void = log.defaultLog,
219
220 fmt_max_depth: usize = fmt.default_max_depth,
221
222 cryptoRandomSeed: fn (buffer: []u8) void = @import("crypto/tlcsprng.zig").defaultRandomSeed,
223
224 crypto_always_getrandom: bool = false,
225
226 crypto_fork_safety: bool = true,
271227
272228 /// By default Zig disables SIGPIPE by setting a "no-op" handler for it. Set this option
273229 /// to `true` to prevent that.
......@@ -280,35 +236,22 @@ pub const options = struct {
280236 /// cases it's unclear why the process was terminated. By capturing SIGPIPE instead, functions that
281237 /// write to broken pipes will return the EPIPE error (error.BrokenPipe) and the program can handle
282238 /// it like any other error.
283 pub const keep_sigpipe: bool = if (@hasDecl(options_override, "keep_sigpipe"))
284 options_override.keep_sigpipe
285 else
286 false;
239 keep_sigpipe: bool = false,
287240
288241 /// By default, std.http.Client will support HTTPS connections. Set this option to `true` to
289242 /// disable TLS support.
290243 ///
291244 /// This will likely reduce the size of the binary, but it will also make it impossible to
292245 /// make a HTTPS connection.
293 pub const http_disable_tls = if (@hasDecl(options_override, "http_disable_tls"))
294 options_override.http_disable_tls
295 else
296 false;
297
298 pub const side_channels_mitigations: crypto.SideChannelsMitigations = if (@hasDecl(options_override, "side_channels_mitigations"))
299 options_override.side_channels_mitigations
300 else
301 crypto.default_side_channels_mitigations;
246 http_disable_tls: bool = false,
247
248 side_channels_mitigations: crypto.SideChannelsMitigations = crypto.default_side_channels_mitigations,
302249};
303250
304251// This forces the start.zig file to be imported, and the comptime logic inside that
305252// file decides whether to export any appropriate start symbols, and call main.
306253comptime {
307254 _ = start;
308
309 for (@typeInfo(options_override).Struct.decls) |decl| {
310 if (!@hasDecl(options, decl.name)) @compileError("no option named " ++ decl.name);
311 }
312255}
313256
314257test {
lib/test_runner.zig+3-3
......@@ -3,9 +3,9 @@ const std = @import("std");
33const io = std.io;
44const builtin = @import("builtin");
55
6pub const std_options = struct {
7 pub const io_mode: io.Mode = builtin.test_io_mode;
8 pub const logFn = log;
6pub const std_options = .{
7 .io_mode = builtin.test_io_mode,
8 .logFn = log,
99};
1010
1111var log_err_count: usize = 0;
src/main.zig+6-6
......@@ -29,16 +29,16 @@ const AstGen = @import("AstGen.zig");
2929const mingw = @import("mingw.zig");
3030const Server = std.zig.Server;
3131
32pub const std_options = struct {
33 pub const wasiCwd = wasi_cwd;
34 pub const logFn = log;
35 pub const enable_segfault_handler = false;
32pub const std_options = .{
33 .wasiCwd = wasi_cwd,
34 .logFn = log,
35 .enable_segfault_handler = false,
3636
37 pub const log_level: std.log.Level = switch (builtin.mode) {
37 .log_level = switch (builtin.mode) {
3838 .Debug => .debug,
3939 .ReleaseSafe, .ReleaseFast => .info,
4040 .ReleaseSmall => .err,
41 };
41 },
4242};
4343
4444// Crash report needs to override the panic handler
test/compare_output.zig+8-8
......@@ -440,14 +440,14 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
440440 cases.add("std.log per scope log level override",
441441 \\const std = @import("std");
442442 \\
443 \\pub const std_options = struct {
444 \\ pub const log_level: std.log.Level = .debug;
443 \\pub const std_options = .{
444 \\ .log_level = .debug,
445445 \\
446 \\ pub const log_scope_levels = &[_]std.log.ScopeLevel{
446 \\ .log_scope_levels = &.{
447447 \\ .{ .scope = .a, .level = .warn },
448448 \\ .{ .scope = .c, .level = .err },
449 \\ };
450 \\ pub const logFn = log;
449 \\ },
450 \\ .logFn = log,
451451 \\};
452452 \\
453453 \\const loga = std.log.scoped(.a);
......@@ -497,9 +497,9 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
497497 cases.add("std.heap.LoggingAllocator logs to std.log",
498498 \\const std = @import("std");
499499 \\
500 \\pub const std_options = struct {
501 \\ pub const log_level: std.log.Level = .debug;
502 \\ pub const logFn = log;
500 \\pub const std_options = .{
501 \\ .log_level = .debug,
502 \\ .logFn = log,
503503 \\};
504504 \\
505505 \\pub fn main() !void {
test/src/Cases.zig+2-2
......@@ -1207,8 +1207,8 @@ const WaitGroup = std.Thread.WaitGroup;
12071207const build_options = @import("build_options");
12081208const Package = @import("../../src/Package.zig");
12091209
1210pub const std_options = struct {
1211 pub const log_level: std.log.Level = .err;
1210pub const std_options = .{
1211 .log_level = .err,
12121212};
12131213
12141214var general_purpose_allocator = std.heap.GeneralPurposeAllocator(.{
test/standalone/http.zig+2-2
......@@ -7,8 +7,8 @@ const Client = http.Client;
77const mem = std.mem;
88const testing = std.testing;
99
10pub const std_options = struct {
11 pub const http_disable_tls = true;
10pub const std_options = .{
11 .http_disable_tls = true,
1212};
1313
1414const max_header_size = 8192;
test/standalone/issue_7030.zig+2-2
......@@ -1,7 +1,7 @@
11const std = @import("std");
22
3pub const std_options = struct {
4 pub const logFn = log;
3pub const std_options = .{
4 .logFn = log,
55};
66
77pub fn log(
test/standalone/issue_9693/main.zig deleted-4
......@@ -1,4 +0,0 @@
1pub const std_options = struct {
2 pub const io_mode = .evented;
3};
4pub fn main() void {}
test/standalone/sigpipe/breakpipe.zig+5-5
......@@ -1,11 +1,11 @@
11const std = @import("std");
22const build_options = @import("build_options");
33
4pub const std_options = if (build_options.keep_sigpipe) struct {
5 pub const keep_sigpipe = true;
6} else struct {
7 // intentionally not setting keep_sigpipe to ensure the default behavior is equivalent to false
8};
4pub usingnamespace if (build_options.keep_sigpipe) struct {
5 pub const std_options = .{
6 .keep_sigpipe = true,
7 };
8} else struct {};
99
1010pub fn main() !void {
1111 const pipe = try std.os.pipe();