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

Conversation

JakubOnderka
Copy link
Contributor

It is easier and it should be also faster, because to_ne_bytes just calls mem::transmute.

It is easier and it should be also faster, because `to_ne_bytes` just calls `mem::transmute`.
@rust-highfive
Copy link
Collaborator

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @TimNN (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see the contribution instructions for more information.

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jan 18, 2019
@JakubOnderka JakubOnderka changed the title Use to_ne_bytes for converting IPv4Address to octets Use to_ne_bytes for converting IPv4Addr to octets Jan 18, 2019
@@ -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.)

@frewsxcv frewsxcv added the T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. label Jan 21, 2019
@Dylan-DPC-zz
Copy link

r? @scottmcm

@rust-highfive rust-highfive assigned scottmcm and unassigned TimNN Feb 4, 2019
@scottmcm
Copy link
Member

scottmcm commented Feb 8, 2019

@bors r+ rollup

@bors
Copy link
Contributor

bors commented Feb 8, 2019

📌 Commit 87f5a98 has been approved by scottmcm

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Feb 8, 2019
pietroalbini added a commit to pietroalbini/rust that referenced this pull request Feb 10, 2019
…r=scottmcm

Use `to_ne_bytes` for converting IPv4Addr to octets

It is easier and it should be also faster, because [`to_ne_bytes`](https://doc.rust-lang.org/std/primitive.u32.html#method.to_ne_bytes) just calls `mem::transmute`.
@tesuji
Copy link
Contributor

tesuji commented Feb 10, 2019

Could we run a perf for this PR?

Mark-Simulacrum added a commit to Mark-Simulacrum/rust that referenced this pull request Feb 10, 2019
…r=scottmcm

Use `to_ne_bytes` for converting IPv4Addr to octets

It is easier and it should be also faster, because [`to_ne_bytes`](https://doc.rust-lang.org/std/primitive.u32.html#method.to_ne_bytes) just calls `mem::transmute`.
GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this pull request Feb 10, 2019
…r=scottmcm

Use `to_ne_bytes` for converting IPv4Addr to octets

It is easier and it should be also faster, because [`to_ne_bytes`](https://doc.rust-lang.org/std/primitive.u32.html#method.to_ne_bytes) just calls `mem::transmute`.
bors added a commit that referenced this pull request Feb 10, 2019
Rollup of 16 pull requests

Successful merges:

 - #57259 (Update reference of rlibc crate to compiler-builtins crate)
 - #57740 (Use `to_ne_bytes` for converting IPv4Addr to octets)
 - #57926 (Tiny expansion to docs for `core::convert`.)
 - #58157 (Add Cargo.lock automatically adding message)
 - #58203 (rustdoc: display sugared return types for async functions)
 - #58243 (Add trait alias support in rustdoc)
 - #58262 (Add #[must_use] message to Fn* traits)
 - #58295 (std::sys::unix::stdio: explain why we do into_raw)
 - #58297 (Cleanup JS a bit)
 - #58317 (Some writing improvement, conciseness of intro)
 - #58324 (miri: give non-generic functions a stable address)
 - #58332 (operand-to-place copies should never be overlapping)
 - #58345 (When there are multiple filenames, print what got interpreted as filenames)
 - #58346 (rpath computation: explain why we pop())
 - #58350 (Fix failing tidy (line endings on Windows))
 - #58352 (miri value visitor: use `?` in macro)

Failed merges:

r? @ghost
@bors bors merged commit 87f5a98 into rust-lang:master Feb 11, 2019
@scottmcm
Copy link
Member

@lzutao There's no need; the ASM (at least on x86) is identical. And the perf benchmarks wouldn't find a difference regardless, since none of them use this enough to show up.

scottmcm added a commit to scottmcm/rust that referenced this pull request Feb 12, 2019
Now that we have {to|from}_be_bytes the code can be simpler.

(Inspired by PR rust-lang#57740)
kennytm added a commit to kennytm/rust that referenced this pull request Feb 18, 2019
…=oli-obk

Use less explicit shifting in std::net::ip

Now that we have `{to|from}_be_bytes` the code can be simpler.

(Inspired by PR rust-lang#57740)
Mark-Simulacrum added a commit to Mark-Simulacrum/rust that referenced this pull request Feb 18, 2019
…=oli-obk

Use less explicit shifting in std::net::ip

Now that we have `{to|from}_be_bytes` the code can be simpler.

(Inspired by PR rust-lang#57740)
kennytm added a commit to kennytm/rust that referenced this pull request Feb 20, 2019
…=oli-obk

Use less explicit shifting in std::net::ip

Now that we have `{to|from}_be_bytes` the code can be simpler.

(Inspired by PR rust-lang#57740)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants