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

Use to_ne_bytes for converting IPv4Addr to octets #57740

Merged
merged 1 commit into from
Feb 11, 2019
Merged
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
3 changes: 1 addition & 2 deletions src/libstd/net/ip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -393,8 +393,7 @@ impl Ipv4Addr {
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn octets(&self) -> [u8; 4] {
let bits = u32::from_be(self.inner.s_addr);
[(bits >> 24) as u8, (bits >> 16) as u8, (bits >> 8) as u8, bits as u8]
self.inner.s_addr.to_ne_bytes()
Copy link
Member

@scottmcm scottmcm Jan 21, 2019

Choose a reason for hiding this comment

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

It's surprising to me that there was an explicit big-endian mention here before, but now it's using native-endian. Obviously the doctest is passing on the little-endian test machine, but are we sure this is correct? Is it possible that, for clarity, it should be written differently? Maybe

let bits = u32::from_be(self.inner.s_addr);
bits.to_be_bytes()

(It's still fully efficient, as LLVM knows that bswap(bswap(x)) == x.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, because s_addr is always in big endian order and octets methods returns data in big endian order too. So we just wanna return bytes as they are and yes, in that case is to_ne_bytes little lie. But I am not sure if your proposed solution will more clarify that, maybe write some comment?

Copy link
Member

Choose a reason for hiding this comment

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

If it's right that's the important thing :)

(A comment with your first sentence there might be nice, though. Up to you.)

}

/// Returns [`true`] for the special 'unspecified' address (0.0.0.0).
Expand Down