-
-
Notifications
You must be signed in to change notification settings - Fork 3k
add std.os.shutdown function for sockets #7245
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
Conversation
a0563e5
to
b1cafa8
Compare
b1cafa8
to
c2c88ad
Compare
1cc51e6
to
46bcd72
Compare
lib/std/os/linux.zig
Outdated
} | ||
|
||
pub fn shutdown(fd: i32, how: i32) usize { | ||
pub fn shutdown(fd: i32, how: c_int) usize { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Native zig types are preferred, why this change?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok I've removed this change and modified the new declaration in c.zig
to use i32
as well.
lib/std/os.zig
Outdated
SystemResources | ||
} || UnexpectedError; | ||
|
||
pub const ShutdownHow = enum { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd make this a simple Zig enum enum { read_end, write_end, both_ends }
and convert this to the proper flag value in fn shutdown
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I dunno, this seems simpler than adding conversion logic.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a leaky abstraction. The "conversion" code is a simple switch, the same you're doing here with a lot of if
s.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok I've changed it to switches internal to the function. This seems like it could be adding unnecessary overhead to me, but not a big deal.
4d4eddc
to
2bca2e4
Compare
lib/std/c.zig
Outdated
pub extern "c" fn uname(buf: *utsname) c_int; | ||
|
||
pub extern "c" fn gethostname(name: [*]u8, len: usize) c_int; | ||
pub extern "c" fn shutdown(socket: fd_t, how: i32) usize; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for how
, I would use a c_int
instead of i32
. c_int
could have different size depending the platform.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and I think also the return value is wrong: usize
is unsigned, whereas POSIX shutdown()
could return -1
on error. I would use c_int
here too 😄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh, I see @LemonBoy said the opposite before for lib/std/os/linux.zig function.
That's the Linux syscall wrapper, we're directly talking with the kernel there and don't give a damn about C/libc, the types there should be Zig's native ones.
The c.zig
definitions, on the other hand, should use the C types instead to guarantee the ABI compatibility.
and I think also the return value is wrong: usize is unsigned, whereas POSIX shutdown() could return -1 on error. I would use c_int here too
Good catch, c_int
is the correct return type.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok I've modified the one in c.zig
to:
pub extern "c" fn shutdown(socket: fd_t, how: c_int) c_int;
2bca2e4
to
af5e066
Compare
af5e066
to
36ee88d
Compare
No description provided.