Skip to content

Commit 782cc58

Browse files
committed
core/net: add Ipv[46]Addr::from_octets, Ipv6Addr::from_segments.
1 parent 1a5a224 commit 782cc58

File tree

2 files changed

+103
-1
lines changed

2 files changed

+103
-1
lines changed

library/core/src/net/ip_addr.rs

+80
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,25 @@ impl Ipv4Addr {
600600
self.octets
601601
}
602602

603+
/// Creates an `Ipv4Addr` from a four element byte array.
604+
///
605+
/// # Examples
606+
///
607+
/// ```
608+
/// #![feature(ip)]
609+
/// use std::net::Ipv4Addr;
610+
///
611+
/// let addr = Ipv4Addr::from_octets([13u8, 12u8, 11u8, 10u8]);
612+
/// assert_eq!(Ipv4Addr::new(13, 12, 11, 10), addr);
613+
/// ```
614+
#[rustc_const_unstable(feature = "const_ipv4", issue = "76205")]
615+
#[unstable(feature = "ip", issue = "27709")]
616+
#[must_use]
617+
#[inline]
618+
pub const fn from_octets(octets: [u8; 4]) -> Ipv4Addr {
619+
Ipv4Addr { octets }
620+
}
621+
603622
/// Returns [`true`] for the special 'unspecified' address (`0.0.0.0`).
604623
///
605624
/// This property is defined in _UNIX Network Programming, Second Edition_,
@@ -1400,6 +1419,37 @@ impl Ipv6Addr {
14001419
]
14011420
}
14021421

1422+
/// Creates an `Ipv6Addr` from an eight element 16-bit array.
1423+
///
1424+
/// # Examples
1425+
///
1426+
/// ```
1427+
/// #![feature(ip)]
1428+
/// use std::net::Ipv6Addr;
1429+
///
1430+
/// let addr = Ipv6Addr::from_segments([
1431+
/// 525u16, 524u16, 523u16, 522u16,
1432+
/// 521u16, 520u16, 519u16, 518u16,
1433+
/// ]);
1434+
/// assert_eq!(
1435+
/// Ipv6Addr::new(
1436+
/// 0x20d, 0x20c,
1437+
/// 0x20b, 0x20a,
1438+
/// 0x209, 0x208,
1439+
/// 0x207, 0x206
1440+
/// ),
1441+
/// addr
1442+
/// );
1443+
/// ```
1444+
#[rustc_const_unstable(feature = "const_ipv4", issue = "76205")]
1445+
#[unstable(feature = "ip", issue = "27709")]
1446+
#[must_use]
1447+
#[inline]
1448+
pub const fn from_segments(segments: [u16; 8]) -> Ipv6Addr {
1449+
let [a, b, c, d, e, f, g, h] = segments;
1450+
Ipv6Addr::new(a, b, c, d, e, f, g, h)
1451+
}
1452+
14031453
/// Returns [`true`] for the special 'unspecified' address (`::`).
14041454
///
14051455
/// This property is defined in [IETF RFC 4291].
@@ -1941,6 +1991,36 @@ impl Ipv6Addr {
19411991
pub const fn octets(&self) -> [u8; 16] {
19421992
self.octets
19431993
}
1994+
1995+
/// Creates an `Ipv6Addr` from a sixteen element byte array.
1996+
///
1997+
/// # Examples
1998+
///
1999+
/// ```
2000+
/// #![feature(ip)]
2001+
/// use std::net::Ipv6Addr;
2002+
///
2003+
/// let addr = Ipv6Addr::from_octets([
2004+
/// 25u8, 24u8, 23u8, 22u8, 21u8, 20u8, 19u8, 18u8,
2005+
/// 17u8, 16u8, 15u8, 14u8, 13u8, 12u8, 11u8, 10u8,
2006+
/// ]);
2007+
/// assert_eq!(
2008+
/// Ipv6Addr::new(
2009+
/// 0x1918, 0x1716,
2010+
/// 0x1514, 0x1312,
2011+
/// 0x1110, 0x0f0e,
2012+
/// 0x0d0c, 0x0b0a
2013+
/// ),
2014+
/// addr
2015+
/// );
2016+
/// ```
2017+
#[rustc_const_unstable(feature = "const_ipv4", issue = "76205")]
2018+
#[unstable(feature = "ip", issue = "27709")]
2019+
#[must_use]
2020+
#[inline]
2021+
pub const fn from_octets(octets: [u8; 16]) -> Ipv6Addr {
2022+
Ipv6Addr { octets }
2023+
}
19442024
}
19452025

19462026
/// Writes an Ipv6Addr, conforming to the canonical style described by

library/core/tests/net/ip_addr.rs

+23-1
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,7 @@ fn ipv6_properties() {
494494
let octets = &[$($octet),*];
495495
assert_eq!(&ip!($s).octets(), octets);
496496
assert_eq!(Ipv6Addr::from(*octets), ip!($s));
497+
assert_eq!(Ipv6Addr::from_octets(*octets), ip!($s));
497498

498499
let unspecified: u32 = 1 << 0;
499500
let loopback: u32 = 1 << 1;
@@ -846,15 +847,19 @@ fn ipv6_from_constructors() {
846847

847848
#[test]
848849
fn ipv4_from_octets() {
849-
assert_eq!(Ipv4Addr::from([127, 0, 0, 1]), Ipv4Addr::new(127, 0, 0, 1))
850+
assert_eq!(Ipv4Addr::from([127, 0, 0, 1]), Ipv4Addr::new(127, 0, 0, 1));
851+
assert_eq!(Ipv4Addr::from_octets([127, 0, 0, 1]), Ipv4Addr::new(127, 0, 0, 1));
850852
}
851853

852854
#[test]
853855
fn ipv6_from_segments() {
854856
let from_u16s =
855857
Ipv6Addr::from([0x0011, 0x2233, 0x4455, 0x6677, 0x8899, 0xaabb, 0xccdd, 0xeeff]);
858+
let from_u16s_explicit =
859+
Ipv6Addr::from([0x0011, 0x2233, 0x4455, 0x6677, 0x8899, 0xaabb, 0xccdd, 0xeeff]);
856860
let new = Ipv6Addr::new(0x0011, 0x2233, 0x4455, 0x6677, 0x8899, 0xaabb, 0xccdd, 0xeeff);
857861
assert_eq!(new, from_u16s);
862+
assert_eq!(new, from_u16s_explicit);
858863
}
859864

860865
#[test]
@@ -865,7 +870,15 @@ fn ipv6_from_octets() {
865870
0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee,
866871
0xff,
867872
]);
873+
let from_u16s_explicit =
874+
Ipv6Addr::from_segments([0x0011, 0x2233, 0x4455, 0x6677, 0x8899, 0xaabb, 0xccdd, 0xeeff]);
875+
let from_u8s_explicit = Ipv6Addr::from_octets([
876+
0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee,
877+
0xff,
878+
]);
868879
assert_eq!(from_u16s, from_u8s);
880+
assert_eq!(from_u16s, from_u16s_explicit);
881+
assert_eq!(from_u16s_explicit, from_u8s_explicit);
869882
}
870883

871884
#[test]
@@ -915,6 +928,9 @@ fn ipv4_const() {
915928
const OCTETS: [u8; 4] = IP_ADDRESS.octets();
916929
assert_eq!(OCTETS, [127, 0, 0, 1]);
917930

931+
const FROM_OCTETS: Ipv4Addr = Ipv4Addr::from_octets(OCTETS);
932+
assert_eq!(IP_ADDRESS, FROM_OCTETS);
933+
918934
const IS_UNSPECIFIED: bool = IP_ADDRESS.is_unspecified();
919935
assert!(!IS_UNSPECIFIED);
920936

@@ -971,9 +987,15 @@ fn ipv6_const() {
971987
const SEGMENTS: [u16; 8] = IP_ADDRESS.segments();
972988
assert_eq!(SEGMENTS, [0, 0, 0, 0, 0, 0, 0, 1]);
973989

990+
const FROM_SEGMENTS: Ipv6Addr = Ipv6Addr::from_segments(SEGMENTS);
991+
assert_eq!(IP_ADDRESS, FROM_SEGMENTS);
992+
974993
const OCTETS: [u8; 16] = IP_ADDRESS.octets();
975994
assert_eq!(OCTETS, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]);
976995

996+
const FROM_OCTETS: Ipv6Addr = Ipv6Addr::from_octets(OCTETS);
997+
assert_eq!(IP_ADDRESS, FROM_OCTETS);
998+
977999
const IS_UNSPECIFIED: bool = IP_ADDRESS.is_unspecified();
9781000
assert!(!IS_UNSPECIFIED);
9791001

0 commit comments

Comments
 (0)