-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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 functions to convert IPv4 to long and back #24652
Conversation
(rust_highfive has picked a reviewer for you, use r? to override) |
} | ||
|
||
/// Converts a 32 bit int to a valid Ipv4Addr | ||
pub fn from_int(&self, ip: u32) -> Ipv4Addr { |
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.
Looks like impl From<u32> for Ipv4Addr
would be better suited than a method.
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.
Why does this even take a &self
?
@nagisa thanks for the review, fixing. |
Still WIP, not ready for review |
@alexcrichton sorry, I misunderstood you in the other PR. I thought you were asking about the implementation. I've been working on implementing CIDR handling functionality in Rust. Converting an IP to and int is a convenient way to work with IP ranges. And I thought these functions might be useful to others too. Another example is, say creating a hash table of IPs. |
Hm, I'm primarily worried about endianness here as there's no clear indication of what order each |
@alexcrichton I do hear your concern around endianness. I guess I can make these functions local to my crate and use those. But, can't I take care of endianness in the functions and have those here? |
Whatever is done can certainly be documented here clearly, but there's not necessarily "one true ordering" to use here. I'm also not sure how common this desire it? LLVM should produce the same code in either case, and it seems like a relatively rare operation to want to go from |
The endianess here should be the one that entering the IP address as u32
|
@alexcrichton I don't think going from IP to @nagisa thanks for pointing out I can use an IP in the browser, pretty cool! |
@alexcrichton @nagisa gentle reminder on this :) I realized that the implementation is broken right now, I will fix it once we reach an agreement if this is needed or not. |
What would be the closest equivalent for IPv6? |
@Diggsey u128, but we haven’t it yet. Anyway, to me it seems fine to land this (given it works and all comments are addressed). I don’t have the power to make the call, though. |
Yes, |
I’d just wait until rust-lang/rfcs#521 is resolved before considering IPv6 equivalent of this functionality, honestly. |
Makes sense. |
Ok, looking at this again and after talking it over with @aturon, I think this is fine to land for now. Unfortunately this means that the |
@@ -244,6 +243,20 @@ impl FromInner<libc::in_addr> for Ipv4Addr { | |||
} | |||
} | |||
|
|||
impl Into<u32> for Ipv4Addr { |
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.
Due to the way Into
and From
works, this is more conventionally written as impl From<Ipv4Addr> for u32
@alexcrichton ready for review now. |
⌛ Testing commit 285ab02 with merge ff71187... |
@bors: retry force clean |
No description provided.