-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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.os.foo functions return usize error codes instead of error unions #17870
Comments
One complication is that the full set of errors from a syscall is practically unknowable, and may change based on the use-case. Because of this, 'forcing' users of direct syscall functions to handle the open-endedness of the return value is a slight benefit of how things are currently IMO. I would also point out that the intended way to use the direct syscall functions is to use Lines 1499 to 1523 in e9a6197
Also worth noting that the same pattern (switch on enum) applies to Windows APIs, see the Lines 390 to 400 in e9a6197
Anyway, it might make sense to have essentially three layers. Using the
|
@squeek502 I think the interface you suggested is quite good, maybe renaming the namespace from
However I am having trouble understanding this point, can you give me as an example a use case where one would like to manually switch between the usize options, instead of receiving all the options in an error set and using standard error handling? If there will be a 1-1 correspondence between errno and the error set it seems like it's impossible to get an unexpected return value (unless the OS is acting silly). I think your proposal is good but I am failing to understand the use cases for |
The thing I had in mind would be syscalls that can be used for many purposes, where each use case could have a different error set / take different parameter types / return different things. One example I could find of this type of thing is the Lines 6941 to 6961 in e74ced2
And there's another usage within Lines 3275 to 3280 in e74ced2
which could be moved into its own This type of thing is much more relevant for Windows APIs, which can have very disparate and non-overlapping usages for the same function. See #14842 for an example of where a generic wrapper was removed in favor of direct syscalls (or more specific wrappers in the future).
|
@squeek502 Actually I didn't know about those windows APIs, that's a good point. But what will be the problem for windows APIs like |
This is one reason I'm a bit skeptical of this approach. Such a function would not really provide much benefit over the current enum return, since the correct way to use it would always be a The non-exhaustive-enum part of Not sure if I fully follow your |
I tried writing some code that used Linux-specific syscalls (specifically
mount
) and was surprised that the implementation of these syscalls returned actualusize
integers as return codes and not zig error-sets as with most other functions instd
. I will try and present my objections, and hope someone will refute them.I tried looked into it and found #2380 which reorganized
std.os
, and it states:I personally do not understand the reasons for the choice of making std.os.linux syscall return a number instead of an error set, and would like someone to explain to me why it makes sense. It degrades syscall error handling to a C-style error handling style. And so for system-specific syscalls Zig shares the faults of the C error handling system (or at least up to ignoring return values):
This style of course half-implicitly ignores any errors returned from these without any
try
keywords. This leads people who write code which heavily uses OS-specific interfaces to write a wrapper file with error-set wrappers like this one with code that should honestly be instd
.Another problem created which one might notice in the above code is the current interface creates a lot of redundant casting, it would make more sense for
std.os.linux.open
to return ani32
type with an error set instead of ausize
.A supposedly correct program which at least panics on errors (not even gracefully handles cases) from these functions will look like this:
Which, for me feels too unziggy. I would like to understand the reasons for this design which (for me at least) feels like it makes it harder to write code which uses syscalls and propose that a change should be made to these functions.
The text was updated successfully, but these errors were encountered: