-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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 BITS, from_bits, to_bits to IP addresses #113746
Conversation
r? @thomcc (rustbot has picked a reviewer for you, use r? to override) |
This comment has been minimized.
This comment has been minimized.
library/core/src/net/ip_addr.rs
Outdated
#[stable(feature = "ip_u32", since = "1.1.0")] | ||
impl From<Ipv4Addr> for u32 { | ||
/// Converts an `Ipv4Addr` into a host byte order `u32`. | ||
/// | ||
/// # Examples | ||
/// | ||
/// ``` | ||
/// use std::net::Ipv4Addr; | ||
/// | ||
/// let addr = Ipv4Addr::new(0x12, 0x34, 0x56, 0x78); | ||
/// assert_eq!(0x12345678, u32::from(addr)); | ||
/// ``` | ||
#[inline] | ||
fn from(ip: Ipv4Addr) -> u32 { | ||
u32::from_be_bytes(ip.octets) | ||
ip.to_bits() | ||
} | ||
} | ||
|
||
#[stable(feature = "ip_u32", since = "1.1.0")] | ||
impl From<u32> for Ipv4Addr { | ||
/// Converts a host byte order `u32` into an `Ipv4Addr`. | ||
/// | ||
/// # Examples | ||
/// | ||
/// ``` | ||
/// use std::net::Ipv4Addr; | ||
/// | ||
/// let addr = Ipv4Addr::from(0x12345678); | ||
/// assert_eq!(Ipv4Addr::new(0x12, 0x34, 0x56, 0x78), addr); | ||
/// ``` | ||
#[inline] | ||
fn from(ip: u32) -> Ipv4Addr { | ||
Ipv4Addr { octets: ip.to_be_bytes() } | ||
Ipv4Addr::from_bits(ip) | ||
} | ||
} |
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 removed the doc comments here since they're effectively moved to the from_bits
and to_bits
methods, and I assumed that the primary reason for adding doc comments to these was due to the lack of a dedicated method.
Would be fine adding them back though.
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 think it's nice when From
impls document how they convert it, especially when it's not completely obvious. Perhaps you could link to from_bits
?
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.
Fair enough; does the latest change seem sufficient?
This has ACP+ right? The impl looks fine to me. |
I have the impression this is fine to land by libs-api, so @bors r+ |
⌛ Testing commit b307014 with merge 91a65364125bd1d7a11340b12874805b137289bd... |
💔 Test failed - checks-actions |
Appears to be an error with the runner itself. |
@bors retry |
☀️ Test successful - checks-actions |
Finished benchmarking commit (8164cdb): comparison URL. Overall result: no relevant changes - no action needed@rustbot label: -perf-regression Instruction countThis benchmark run did not return any relevant results for this metric. Max RSS (memory usage)ResultsThis is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.
CyclesResultsThis is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.
Binary sizeThis benchmark run did not return any relevant results for this metric. Bootstrap: 650.262s -> 651.03s (0.12%) |
Host byte order is the system byte order, as opposed to network byte order (big endian). I would accept a docs PR clarifying the endianness conversions, though. |
@thomcc I just copied the docs from the |
ACP: rust-lang/libs-team#235
Tracking issue: #113744