Skip to content

Make IpvXAddr::new const fns and the well known addresses associated constants #52872

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

Merged
merged 8 commits into from
Aug 8, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Make Ipv{4,6}Addr::new const fns
  • Loading branch information
faern committed Aug 8, 2018
commit 02c272db2d3fd63f564be43ca6b8b76c9ce42186
2 changes: 2 additions & 0 deletions src/libstd/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@
#![feature(char_error_internals)]
#![feature(compiler_builtins_lib)]
#![feature(const_fn)]
#![feature(const_int_ops)]
#![feature(core_intrinsics)]
#![feature(dropck_eyepatch)]
#![feature(exact_size_is_empty)]
Expand Down Expand Up @@ -281,6 +282,7 @@
#![feature(ptr_internals)]
#![feature(raw)]
#![feature(rustc_attrs)]
#![feature(rustc_const_unstable)]
#![feature(std_internals)]
#![feature(stdsimd)]
#![feature(shrink_to)]
Expand Down
48 changes: 28 additions & 20 deletions src/libstd/net/ip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ use cmp::Ordering;
use fmt;
use hash;
use mem;
use net::{hton, ntoh};
use sys::net::netc as c;
use sys_common::{AsInner, FromInner};

Expand Down Expand Up @@ -340,13 +339,16 @@ impl Ipv4Addr {
/// let addr = Ipv4Addr::new(127, 0, 0, 1);
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn new(a: u8, b: u8, c: u8, d: u8) -> Ipv4Addr {
#[rustc_const_unstable(feature = "const_ip")]
pub const fn new(a: u8, b: u8, c: u8, d: u8) -> Ipv4Addr {
Ipv4Addr {
inner: c::in_addr {
s_addr: hton(((a as u32) << 24) |
((b as u32) << 16) |
((c as u32) << 8) |
(d as u32)),
s_addr: u32::to_be(
((a as u32) << 24) |
((b as u32) << 16) |
((c as u32) << 8) |
(d as u32)
),
}
}
}
Expand Down Expand Up @@ -399,7 +401,7 @@ impl Ipv4Addr {
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn octets(&self) -> [u8; 4] {
let bits = ntoh(self.inner.s_addr);
let bits = u32::from_be(self.inner.s_addr);
[(bits >> 24) as u8, (bits >> 16) as u8, (bits >> 8) as u8, bits as u8]
}

Expand Down Expand Up @@ -763,7 +765,7 @@ impl PartialOrd<IpAddr> for Ipv4Addr {
#[stable(feature = "rust1", since = "1.0.0")]
impl Ord for Ipv4Addr {
fn cmp(&self, other: &Ipv4Addr) -> Ordering {
ntoh(self.inner.s_addr).cmp(&ntoh(other.inner.s_addr))
u32::from_be(self.inner.s_addr).cmp(&u32::from_be(other.inner.s_addr))
}
}

Expand Down Expand Up @@ -856,18 +858,24 @@ impl Ipv6Addr {
/// let addr = Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff);
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn new(a: u16, b: u16, c: u16, d: u16, e: u16, f: u16, g: u16,
h: u16) -> Ipv6Addr {
let mut addr: c::in6_addr = unsafe { mem::zeroed() };
addr.s6_addr = [(a >> 8) as u8, a as u8,
(b >> 8) as u8, b as u8,
(c >> 8) as u8, c as u8,
(d >> 8) as u8, d as u8,
(e >> 8) as u8, e as u8,
(f >> 8) as u8, f as u8,
(g >> 8) as u8, g as u8,
(h >> 8) as u8, h as u8];
Ipv6Addr { inner: addr }
#[rustc_const_unstable(feature = "const_ip")]
pub const fn new(a: u16, b: u16, c: u16, d: u16, e: u16, f: u16,
g: u16, h: u16) -> Ipv6Addr {
Ipv6Addr {
inner: c::in6_addr {
s6_addr: [
(a >> 8) as u8, a as u8,
(b >> 8) as u8, b as u8,
(c >> 8) as u8, c as u8,
(d >> 8) as u8, d as u8,
(e >> 8) as u8, e as u8,
(f >> 8) as u8, f as u8,
(g >> 8) as u8, g as u8,
(h >> 8) as u8, h as u8
],
}
}

}

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