Auto merge of #32050 - achanda:from-slice-v4, r=alexcrichton
Add an impl for From trait Converts a u8 slice to a Ipv4Addr More discussion on this here: https://github.com/rust-lang/rfcs/pull/1498#issuecomment-191921655
This commit is contained in:
@@ -279,6 +279,13 @@ impl From<u32> for Ipv4Addr {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[stable(feature = "from_slice_v4", since = "1.9.0")]
|
||||||
|
impl From<[u8; 4]> for Ipv4Addr {
|
||||||
|
fn from(octets: [u8; 4]) -> Ipv4Addr {
|
||||||
|
Ipv4Addr::new(octets[0], octets[1], octets[2], octets[3])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl Ipv6Addr {
|
impl Ipv6Addr {
|
||||||
/// Creates a new IPv6 address from eight 16-bit segments.
|
/// Creates a new IPv6 address from eight 16-bit segments.
|
||||||
///
|
///
|
||||||
@@ -831,6 +838,11 @@ mod tests {
|
|||||||
assert_eq!(Ipv4Addr::from(2130706433), a);
|
assert_eq!(Ipv4Addr::from(2130706433), a);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn ipv4_from_u32_slice() {
|
||||||
|
assert_eq!(Ipv4Addr::from([127, 0, 0, 1]), Ipv4Addr::new(127, 0, 0, 1))
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn ord() {
|
fn ord() {
|
||||||
assert!(Ipv4Addr::new(100, 64, 3, 3) < Ipv4Addr::new(192, 0, 2, 2));
|
assert!(Ipv4Addr::new(100, 64, 3, 3) < Ipv4Addr::new(192, 0, 2, 2));
|
||||||
|
|||||||
Reference in New Issue
Block a user