You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Many things in the Zig standard library are inherently unportable across operating systems, especially things in std.os, yet they share implementation in a common file. In this case, Windows and Posix both share their implementations in the same file, std.os.child_process.zig. This is in bad taste for several reasons:
because we have to call things like if (is_windows) too much
because we have to void out irrelevant variables (such as GID or PID on Windows)
Solution:
Do a platform based assignment in std/os/index.zig like so:
const std = @import("../index.zig");
const os = std.os;
const windows = os.windows;
const linux = os.linux; // note: this does not exist yet, but it should. info more far down
const macos = os.macos; // note: this does not exist yet, but it should. info more far down
pub const ChildProcess = switch(builtin.os) {
bulitin.Os.windows => windows.child_process,
builtin.Os.linux => linux.child_process,
builtin.Os.macos => macos.child_process,
};
// other stuff
Implement the actual thing in the os-specific directory.
Linux and macos now are to provide their own implementations, and not rely on POSIX. There may be codesharing between POSIX OS's, but they should rewrite it explicitly anyway. Implementation of std.os.linux.child_process:
const std = @import("../index.zig");
const os = std.os
// whatever other imports we need
pub const ChildProcess = struct {
// linux specific spawn, we use clone() instead of fork() as an example (maybe it's faster idk)
pub fn spawn() {
//local implemtation goes here...
}
// other posix stuff
};
Much cleaner to read, and much easier to add a new OS implementation.
The text was updated successfully, but these errors were encountered:
EDIT: Obviously this needs more discussion before I feel good about it, so rip this proposal to shreds. :)
EDIT2: Also, this means every version of std.os must expose exactly the same functions and values, otherwise this will not work. But since std.os only implements portable interfaces, this should not be an issue.
because we have to call things like if (is_windows) too much
Putting the alternate code in another file doesn't make it simpler. We're either going to have these if statements, or duplicated code.
Have a look at std.event.Loop and see what you think. It's a great example of complex code that works on Linux, Windows, and MacOS, but with the same basic structure.
because we have to void out irrelevant variables (such as GID or PID on Windows)
I would argue this makes the implementation cleaner because it shows what exactly is different and what is the same between different operating systems.
Note that we currently do have std.os.linux and std.os.windows and they are the syscall functions. (For Windows it's the kernel32.dll functions).
Take for example
std.os.child_process
:Exhibit A: std/os/child_process.zig:18,21
Exhibit B: std/os/child_process.zig:134,176
Many things in the Zig standard library are inherently unportable across operating systems, especially things in
std.os
, yet they share implementation in a common file. In this case, Windows and Posix both share their implementations in the same file,std.os.child_process.zig
. This is in bad taste for several reasons:if (is_windows)
too muchSolution:
Do a platform based assignment in
std/os/index.zig
like so:Implement the actual thing in the os-specific directory.
Linux and macos now are to provide their own implementations, and not rely on POSIX. There may be codesharing between POSIX OS's, but they should rewrite it explicitly anyway. Implementation of
std.os.linux.child_process
:Much cleaner to read, and much easier to add a new OS implementation.
The text was updated successfully, but these errors were encountered: