Auto merge of #49418 - frewsxcv:frewsxcv-network-order, r=TimNN
Clarify network byte order conversions for integer / IP address conversions. Opened primarily to address https://github.com/rust-lang/rust/issues/48819. Also added a few other conversion docs/examples.
This commit is contained in:
commit
cb1f89864e
1 changed files with 80 additions and 2 deletions
|
@ -778,7 +778,16 @@ impl FromInner<c::in_addr> for Ipv4Addr {
|
|||
|
||||
#[stable(feature = "ip_u32", since = "1.1.0")]
|
||||
impl From<Ipv4Addr> for u32 {
|
||||
/// It performs the conversion in network order (big-endian).
|
||||
/// Convert an `Ipv4Addr` into a host byte order `u32`.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use std::net::Ipv4Addr;
|
||||
///
|
||||
/// let addr = Ipv4Addr::new(13, 12, 11, 10);
|
||||
/// assert_eq!(0x0d0c0b0au32, u32::from(addr));
|
||||
/// ```
|
||||
fn from(ip: Ipv4Addr) -> u32 {
|
||||
let ip = ip.octets();
|
||||
((ip[0] as u32) << 24) + ((ip[1] as u32) << 16) + ((ip[2] as u32) << 8) + (ip[3] as u32)
|
||||
|
@ -787,7 +796,16 @@ impl From<Ipv4Addr> for u32 {
|
|||
|
||||
#[stable(feature = "ip_u32", since = "1.1.0")]
|
||||
impl From<u32> for Ipv4Addr {
|
||||
/// It performs the conversion in network order (big-endian).
|
||||
/// Convert a host byte order `u32` into an `Ipv4Addr`.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use std::net::Ipv4Addr;
|
||||
///
|
||||
/// let addr = Ipv4Addr::from(0x0d0c0b0au32);
|
||||
/// assert_eq!(Ipv4Addr::new(13, 12, 11, 10), addr);
|
||||
/// ```
|
||||
fn from(ip: u32) -> Ipv4Addr {
|
||||
Ipv4Addr::new((ip >> 24) as u8, (ip >> 16) as u8, (ip >> 8) as u8, ip as u8)
|
||||
}
|
||||
|
@ -795,6 +813,14 @@ impl From<u32> for Ipv4Addr {
|
|||
|
||||
#[stable(feature = "from_slice_v4", since = "1.9.0")]
|
||||
impl From<[u8; 4]> for Ipv4Addr {
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use std::net::Ipv4Addr;
|
||||
///
|
||||
/// let addr = Ipv4Addr::from([13u8, 12u8, 11u8, 10u8]);
|
||||
/// assert_eq!(Ipv4Addr::new(13, 12, 11, 10), addr);
|
||||
/// ```
|
||||
fn from(octets: [u8; 4]) -> Ipv4Addr {
|
||||
Ipv4Addr::new(octets[0], octets[1], octets[2], octets[3])
|
||||
}
|
||||
|
@ -802,6 +828,16 @@ impl From<[u8; 4]> for Ipv4Addr {
|
|||
|
||||
#[stable(feature = "ip_from_slice", since = "1.17.0")]
|
||||
impl From<[u8; 4]> for IpAddr {
|
||||
/// Create an `IpAddr::V4` from a four element byte array.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use std::net::{IpAddr, Ipv4Addr};
|
||||
///
|
||||
/// let addr = IpAddr::from([13u8, 12u8, 11u8, 10u8]);
|
||||
/// assert_eq!(IpAddr::V4(Ipv4Addr::new(13, 12, 11, 10)), addr);
|
||||
/// ```
|
||||
fn from(octets: [u8; 4]) -> IpAddr {
|
||||
IpAddr::V4(Ipv4Addr::from(octets))
|
||||
}
|
||||
|
@ -1395,6 +1431,27 @@ impl From<[u16; 8]> for Ipv6Addr {
|
|||
|
||||
#[stable(feature = "ip_from_slice", since = "1.17.0")]
|
||||
impl From<[u8; 16]> for IpAddr {
|
||||
/// Create an `IpAddr::V6` from a sixteen element byte array.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use std::net::{IpAddr, Ipv6Addr};
|
||||
///
|
||||
/// let addr = IpAddr::from([
|
||||
/// 25u8, 24u8, 23u8, 22u8, 21u8, 20u8, 19u8, 18u8,
|
||||
/// 17u8, 16u8, 15u8, 14u8, 13u8, 12u8, 11u8, 10u8,
|
||||
/// ]);
|
||||
/// assert_eq!(
|
||||
/// IpAddr::V6(Ipv6Addr::new(
|
||||
/// 0x1918, 0x1716,
|
||||
/// 0x1514, 0x1312,
|
||||
/// 0x1110, 0x0f0e,
|
||||
/// 0x0d0c, 0x0b0a
|
||||
/// )),
|
||||
/// addr
|
||||
/// );
|
||||
/// ```
|
||||
fn from(octets: [u8; 16]) -> IpAddr {
|
||||
IpAddr::V6(Ipv6Addr::from(octets))
|
||||
}
|
||||
|
@ -1402,6 +1459,27 @@ impl From<[u8; 16]> for IpAddr {
|
|||
|
||||
#[stable(feature = "ip_from_slice", since = "1.17.0")]
|
||||
impl From<[u16; 8]> for IpAddr {
|
||||
/// Create an `IpAddr::V6` from an eight element 16-bit array.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use std::net::{IpAddr, Ipv6Addr};
|
||||
///
|
||||
/// let addr = IpAddr::from([
|
||||
/// 525u16, 524u16, 523u16, 522u16,
|
||||
/// 521u16, 520u16, 519u16, 518u16,
|
||||
/// ]);
|
||||
/// assert_eq!(
|
||||
/// IpAddr::V6(Ipv6Addr::new(
|
||||
/// 0x20d, 0x20c,
|
||||
/// 0x20b, 0x20a,
|
||||
/// 0x209, 0x208,
|
||||
/// 0x207, 0x206
|
||||
/// )),
|
||||
/// addr
|
||||
/// );
|
||||
/// ```
|
||||
fn from(segments: [u16; 8]) -> IpAddr {
|
||||
IpAddr::V6(Ipv6Addr::from(segments))
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue