Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

std: make options a struct instance instead of a namespace #18712

Merged
merged 7 commits into from
Feb 9, 2024

Conversation

Vexu
Copy link
Member

@Vexu Vexu commented Jan 27, 2024

As per #18697 (comment)

Closes #14432

@Vexu Vexu added breaking Implementing this issue could cause existing code to no longer compile or have different behavior. standard library This issue involves writing Zig code for the standard library. labels Jan 27, 2024
@Vexu Vexu force-pushed the std.options branch 4 times, most recently from e2b58fa to 62e562c Compare January 29, 2024 17:41
@andrewrk andrewrk merged commit 54bbc73 into ziglang:master Feb 9, 2024
10 checks passed
@Vexu Vexu deleted the std.options branch February 9, 2024 21:51
@nektro
Copy link
Contributor

nektro commented Feb 10, 2024

missed this when it first came through but imo the name of std.Options does not effectively communicate it is specific to the stdlib

Techatrix added a commit to Techatrix/known-folders that referenced this pull request Feb 10, 2024
The async mode has been removed in ziglang/zig#18712
SuperAuguste pushed a commit to ziglibs/known-folders that referenced this pull request Feb 11, 2024
The async mode has been removed in ziglang/zig#18712
@Arnau478
Copy link
Contributor

As @nektro said, std.Options is quite a bad name if it's std-specific.

As a side proposal, maybe it would be better to create a system for generalized and consistent library options. The std shouldn't be any different than other libraries, after all...

mochalins added a commit to mochalins/zig-network that referenced this pull request Feb 19, 2024
`std.io.is_async` no longer exists since ziglang/zig#18712. Removed all
references for compatibility with latest Zig `master`.
ikskuh pushed a commit to ikskuh/zig-network that referenced this pull request Feb 19, 2024
`std.io.is_async` no longer exists since ziglang/zig#18712. Removed all
references for compatibility with latest Zig `master`.
@kristoff-it
Copy link
Member

Release notes draft:

Std Options

Previously, when one wanted to override defaults, such as the logging function used by std.log, they would have to define std_options in their root file, like so:

pub const std_options = struct {
    pub const logFn = myLogFn;
};

Note how std_options above is a struct type definiton. In this release std_options is now an instance of std.Options, making the process of defining overrides less error-prone.

This is now how the code above would look like now:

pub const std_options: std.Options = .{
    .logFn = myLogFn,
};

And this is the definition of std.Options to see what else you can override.

TODO: explain that you can still override other stuff like the panic function in a different way because that's not a stdlib override

pub const Options = struct {
    enable_segfault_handler: bool = debug.default_enable_segfault_handler,

    /// Function used to implement `std.fs.cwd` for WASI.
    wasiCwd: fn () os.wasi.fd_t = fs.defaultWasiCwd,

    /// The current log level.
    log_level: log.Level = log.default_level,

    log_scope_levels: []const log.ScopeLevel = &.{},

    logFn: fn (
        comptime message_level: log.Level,
        comptime scope: @TypeOf(.enum_literal),
        comptime format: []const u8,
        args: anytype,
    ) void = log.defaultLog,

    fmt_max_depth: usize = fmt.default_max_depth,

    cryptoRandomSeed: fn (buffer: []u8) void = @import("crypto/tlcsprng.zig").defaultRandomSeed,

    crypto_always_getrandom: bool = false,

    crypto_fork_safety: bool = true,

    /// By default Zig disables SIGPIPE by setting a "no-op" handler for it.  Set this option
    /// to `true` to prevent that.
    ///
    /// Note that we use a "no-op" handler instead of SIG_IGN because it will not be inherited by
    /// any child process.
    ///
    /// SIGPIPE is triggered when a process attempts to write to a broken pipe. By default, SIGPIPE
    /// will terminate the process instead of exiting.  It doesn't trigger the panic handler so in many
    /// cases it's unclear why the process was terminated.  By capturing SIGPIPE instead, functions that
    /// write to broken pipes will return the EPIPE error (error.BrokenPipe) and the program can handle
    /// it like any other error.
    keep_sigpipe: bool = false,

    /// By default, std.http.Client will support HTTPS connections.  Set this option to `true` to
    /// disable TLS support.
    ///
    /// This will likely reduce the size of the binary, but it will also make it impossible to
    /// make a HTTPS connection.
    http_disable_tls: bool = false,

    side_channels_mitigations: crypto.SideChannelsMitigations = crypto.default_side_channels_mitigations,
};

@andrewrk andrewrk added release notes This PR should be mentioned in the release notes. and removed release notes draft labels Apr 18, 2024
@andrewrk andrewrk added this to the 0.12.0 milestone Apr 18, 2024
TUSF pushed a commit to TUSF/zig that referenced this pull request May 9, 2024
std: make options a struct instance instead of a namespace
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking Implementing this issue could cause existing code to no longer compile or have different behavior. release notes This PR should be mentioned in the release notes. standard library This issue involves writing Zig code for the standard library.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Make std_options a comptime struct instead of a namespace
5 participants