Skip to content

Commit 02c272d

Browse files
committed
Make Ipv{4,6}Addr::new const fns
1 parent 6f943c0 commit 02c272d

File tree

2 files changed

+30
-20
lines changed

2 files changed

+30
-20
lines changed

src/libstd/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@
252252
#![feature(char_error_internals)]
253253
#![feature(compiler_builtins_lib)]
254254
#![feature(const_fn)]
255+
#![feature(const_int_ops)]
255256
#![feature(core_intrinsics)]
256257
#![feature(dropck_eyepatch)]
257258
#![feature(exact_size_is_empty)]
@@ -281,6 +282,7 @@
281282
#![feature(ptr_internals)]
282283
#![feature(raw)]
283284
#![feature(rustc_attrs)]
285+
#![feature(rustc_const_unstable)]
284286
#![feature(std_internals)]
285287
#![feature(stdsimd)]
286288
#![feature(shrink_to)]

src/libstd/net/ip.rs

+28-20
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ use cmp::Ordering;
1717
use fmt;
1818
use hash;
1919
use mem;
20-
use net::{hton, ntoh};
2120
use sys::net::netc as c;
2221
use sys_common::{AsInner, FromInner};
2322

@@ -340,13 +339,16 @@ impl Ipv4Addr {
340339
/// let addr = Ipv4Addr::new(127, 0, 0, 1);
341340
/// ```
342341
#[stable(feature = "rust1", since = "1.0.0")]
343-
pub fn new(a: u8, b: u8, c: u8, d: u8) -> Ipv4Addr {
342+
#[rustc_const_unstable(feature = "const_ip")]
343+
pub const fn new(a: u8, b: u8, c: u8, d: u8) -> Ipv4Addr {
344344
Ipv4Addr {
345345
inner: c::in_addr {
346-
s_addr: hton(((a as u32) << 24) |
347-
((b as u32) << 16) |
348-
((c as u32) << 8) |
349-
(d as u32)),
346+
s_addr: u32::to_be(
347+
((a as u32) << 24) |
348+
((b as u32) << 16) |
349+
((c as u32) << 8) |
350+
(d as u32)
351+
),
350352
}
351353
}
352354
}
@@ -399,7 +401,7 @@ impl Ipv4Addr {
399401
/// ```
400402
#[stable(feature = "rust1", since = "1.0.0")]
401403
pub fn octets(&self) -> [u8; 4] {
402-
let bits = ntoh(self.inner.s_addr);
404+
let bits = u32::from_be(self.inner.s_addr);
403405
[(bits >> 24) as u8, (bits >> 16) as u8, (bits >> 8) as u8, bits as u8]
404406
}
405407

@@ -763,7 +765,7 @@ impl PartialOrd<IpAddr> for Ipv4Addr {
763765
#[stable(feature = "rust1", since = "1.0.0")]
764766
impl Ord for Ipv4Addr {
765767
fn cmp(&self, other: &Ipv4Addr) -> Ordering {
766-
ntoh(self.inner.s_addr).cmp(&ntoh(other.inner.s_addr))
768+
u32::from_be(self.inner.s_addr).cmp(&u32::from_be(other.inner.s_addr))
767769
}
768770
}
769771

@@ -856,18 +858,24 @@ impl Ipv6Addr {
856858
/// let addr = Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff);
857859
/// ```
858860
#[stable(feature = "rust1", since = "1.0.0")]
859-
pub fn new(a: u16, b: u16, c: u16, d: u16, e: u16, f: u16, g: u16,
860-
h: u16) -> Ipv6Addr {
861-
let mut addr: c::in6_addr = unsafe { mem::zeroed() };
862-
addr.s6_addr = [(a >> 8) as u8, a as u8,
863-
(b >> 8) as u8, b as u8,
864-
(c >> 8) as u8, c as u8,
865-
(d >> 8) as u8, d as u8,
866-
(e >> 8) as u8, e as u8,
867-
(f >> 8) as u8, f as u8,
868-
(g >> 8) as u8, g as u8,
869-
(h >> 8) as u8, h as u8];
870-
Ipv6Addr { inner: addr }
861+
#[rustc_const_unstable(feature = "const_ip")]
862+
pub const fn new(a: u16, b: u16, c: u16, d: u16, e: u16, f: u16,
863+
g: u16, h: u16) -> Ipv6Addr {
864+
Ipv6Addr {
865+
inner: c::in6_addr {
866+
s6_addr: [
867+
(a >> 8) as u8, a as u8,
868+
(b >> 8) as u8, b as u8,
869+
(c >> 8) as u8, c as u8,
870+
(d >> 8) as u8, d as u8,
871+
(e >> 8) as u8, e as u8,
872+
(f >> 8) as u8, f as u8,
873+
(g >> 8) as u8, g as u8,
874+
(h >> 8) as u8, h as u8
875+
],
876+
}
877+
}
878+
871879
}
872880

873881
/// Creates a new IPv6 address representing localhost: `::1`.

0 commit comments

Comments
 (0)