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

Add htonl, htons, ntohl, ntohs #3669

Merged
merged 1 commit into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions libc-test/semver/unix.txt
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,8 @@ grantpt
group
hostent
hstrerror
htonl
htons
if_indextoname
if_nametoindex
in6_addr
Expand Down Expand Up @@ -651,6 +653,8 @@ munmap
nanosleep
nfds_t
nlink_t
ntohl
ntohs
off_t
open
opendir
Expand Down
17 changes: 17 additions & 0 deletions src/unix/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1367,6 +1367,23 @@ extern "C" {

}

safe_f! {
// It seems htonl, etc are macros on macOS. So we have to reimplement them. So let's
// reimplement them for all UNIX platforms
Comment on lines +1371 to +1372
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could split the declaration for that target.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could split the declaration for that target

@JohnTitor , yes, we can. But I think this is slightly more error-prone than my approach. I. e. if we split the declaration, then it is possible we introduce bug for some platform, but not for others

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I generally agree with JohnTitor here that most things should just use the host functions if available, concerns about bugs on one platform but not others isn't much of a concern when one of those platforms uses the system functions.

However, these are also straightforward enough that it doesn't seem worth the effort to split. So I think this is okay.

pub {const} fn htonl(hostlong: u32) -> u32 {
u32::to_be(hostlong)
}
pub {const} fn htons(hostshort: u16) -> u16 {
u16::to_be(hostshort)
}
pub {const} fn ntohl(netlong: u32) -> u32 {
u32::from_be(netlong)
}
pub {const} fn ntohs(netshort: u16) -> u16 {
u16::from_be(netshort)
}
}

cfg_if! {
if #[cfg(not(any(target_os = "emscripten",
target_os = "android",
Expand Down