Skip to content

Commit

Permalink
Auto merge of #52872 - faern:use-modern-alignment-libc, r=TimNN
Browse files Browse the repository at this point in the history
Make IpvXAddr::new const fns and the well known addresses associated constants

Implements/fixes #44582

I just got a PR towards libc (rust-lang/libc#1044) merged. With the new feature added in that PR it is now possible to create `in6_addr` instances as consts. This enables us to finally make the constructors of the IP structs const fns and to make the localhost/unspecified addresses associated constants, as agreed in the above mentioned tracking issue.

I also added a BROADCAST constant. Personally this is the well known address I tend to need the most often.
  • Loading branch information
bors committed Aug 8, 2018
2 parents ffb09df + c0041f4 commit ebe8df4
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 99 deletions.
86 changes: 43 additions & 43 deletions src/Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/liblibc
Submodule liblibc updated 55 files
+13 −6 .travis.yml
+59 −59 Cargo.lock
+2 −1 Cargo.toml
+13 −1 README.md
+6 −6 ci/docker/aarch64-unknown-linux-musl/Dockerfile
+6 −6 ci/docker/arm-unknown-linux-musleabihf/Dockerfile
+7 −6 ci/docker/i686-unknown-linux-musl/Dockerfile
+4 −1 ci/docker/sparc64-unknown-linux-gnu/Dockerfile
+6 −6 ci/docker/x86_64-unknown-linux-musl/Dockerfile
+1 −0 ci/ios/deploy_and_run_on_ios_simulator.rs
+10 −2 ci/run.sh
+1 −0 libc-test/Cargo.toml
+65 −20 libc-test/build.rs
+110 −38 src/fuchsia/mod.rs
+1 −1 src/lib.rs
+19 −2 src/macros.rs
+2 −0 src/redox/net.rs
+0 −2 src/unix/bsd/apple/b64.rs
+13 −10 src/unix/bsd/apple/mod.rs
+24 −0 src/unix/bsd/freebsdlike/dragonfly/mod.rs
+28 −0 src/unix/bsd/freebsdlike/freebsd/mod.rs
+0 −13 src/unix/bsd/netbsdlike/mod.rs
+14 −0 src/unix/bsd/netbsdlike/netbsd/mod.rs
+11 −1 src/unix/bsd/netbsdlike/openbsdlike/mod.rs
+203 −1 src/unix/bsd/netbsdlike/openbsdlike/openbsd/mod.rs
+2 −0 src/unix/hermit/aarch64.rs
+736 −0 src/unix/hermit/mod.rs
+2 −0 src/unix/hermit/x86_64.rs
+13 −2 src/unix/mod.rs
+72 −23 src/unix/newlib/mod.rs
+7 −0 src/unix/notbsd/android/mod.rs
+27 −12 src/unix/notbsd/emscripten.rs
+52 −54 src/unix/notbsd/linux/mips/mips32.rs
+52 −54 src/unix/notbsd/linux/mips/mips64.rs
+5 −0 src/unix/notbsd/linux/mips/mod.rs
+132 −38 src/unix/notbsd/linux/mod.rs
+85 −0 src/unix/notbsd/linux/musl/b32/arm.rs
+85 −0 src/unix/notbsd/linux/musl/b32/mips.rs
+4 −84 src/unix/notbsd/linux/musl/b32/mod.rs
+866 −0 src/unix/notbsd/linux/musl/b32/powerpc.rs
+85 −0 src/unix/notbsd/linux/musl/b32/x86.rs
+2 −1 src/unix/notbsd/linux/musl/mod.rs
+52 −54 src/unix/notbsd/linux/other/b32/mod.rs
+28 −27 src/unix/notbsd/linux/other/b64/aarch64.rs
+52 −54 src/unix/notbsd/linux/other/b64/not_x32.rs
+52 −54 src/unix/notbsd/linux/other/b64/powerpc64.rs
+25 −24 src/unix/notbsd/linux/other/b64/sparc64.rs
+25 −24 src/unix/notbsd/linux/other/b64/x32.rs
+5 −0 src/unix/notbsd/linux/other/mod.rs
+30 −24 src/unix/notbsd/linux/s390x.rs
+1 −0 src/unix/solaris/mod.rs
+5 −0 src/unix/uclibc/mips/mips32.rs
+5 −0 src/unix/uclibc/mips/mips64.rs
+72 −23 src/unix/uclibc/mod.rs
+67 −11 src/unix/uclibc/x86_64/mod.rs
3 changes: 3 additions & 0 deletions src/libstd/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,8 @@
#![feature(char_error_internals)]
#![feature(compiler_builtins_lib)]
#![feature(const_fn)]
#![feature(const_int_ops)]
#![feature(const_ip)]
#![feature(core_intrinsics)]
#![feature(dropck_eyepatch)]
#![feature(exact_size_is_empty)]
Expand Down Expand Up @@ -281,6 +283,7 @@
#![feature(ptr_internals)]
#![feature(raw)]
#![feature(rustc_attrs)]
#![feature(rustc_const_unstable)]
#![feature(std_internals)]
#![feature(stdsimd)]
#![feature(shrink_to)]
Expand Down
121 changes: 68 additions & 53 deletions src/libstd/net/ip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,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,52 +338,67 @@ 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)
),
}
}
}

/// Creates a new IPv4 address with the address pointing to localhost: 127.0.0.1.
/// An IPv4 address with the address pointing to localhost: 127.0.0.1.
///
/// # Examples
///
/// ```
/// #![feature(ip_constructors)]
/// use std::net::Ipv4Addr;
///
/// let addr = Ipv4Addr::localhost();
/// let addr = Ipv4Addr::LOCALHOST;
/// assert_eq!(addr, Ipv4Addr::new(127, 0, 0, 1));
/// ```
#[unstable(feature = "ip_constructors",
reason = "requires greater scrutiny before stabilization",
issue = "44582")]
pub fn localhost() -> Ipv4Addr {
Ipv4Addr::new(127, 0, 0, 1)
}
pub const LOCALHOST: Self = Ipv4Addr::new(127, 0, 0, 1);

/// Creates a new IPv4 address representing an unspecified address: 0.0.0.0
/// An IPv4 address representing an unspecified address: 0.0.0.0
///
/// # Examples
///
/// ```
/// #![feature(ip_constructors)]
/// use std::net::Ipv4Addr;
///
/// let addr = Ipv4Addr::unspecified();
/// let addr = Ipv4Addr::UNSPECIFIED;
/// assert_eq!(addr, Ipv4Addr::new(0, 0, 0, 0));
/// ```
#[unstable(feature = "ip_constructors",
reason = "requires greater scrutiny before stabilization",
issue = "44582")]
pub fn unspecified() -> Ipv4Addr {
Ipv4Addr::new(0, 0, 0, 0)
}
pub const UNSPECIFIED: Self = Ipv4Addr::new(0, 0, 0, 0);

/// An IPv4 address representing the broadcast address: 255.255.255.255
///
/// # Examples
///
/// ```
/// #![feature(ip_constructors)]
/// use std::net::Ipv4Addr;
///
/// let addr = Ipv4Addr::BROADCAST;
/// assert_eq!(addr, Ipv4Addr::new(255, 255, 255, 255));
/// ```
#[unstable(feature = "ip_constructors",
reason = "requires greater scrutiny before stabilization",
issue = "44582")]
pub const BROADCAST: Self = Ipv4Addr::new(255, 255, 255, 255);

/// Returns the four eight-bit integers that make up this address.
///
Expand All @@ -399,7 +412,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 @@ -573,8 +586,7 @@ impl Ipv4Addr {
/// ```
#[stable(since = "1.7.0", feature = "ip_17")]
pub fn is_broadcast(&self) -> bool {
self.octets()[0] == 255 && self.octets()[1] == 255 &&
self.octets()[2] == 255 && self.octets()[3] == 255
self == &Self::BROADCAST
}

/// Returns [`true`] if this address is in a range designated for documentation.
Expand Down Expand Up @@ -763,7 +775,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,55 +868,57 @@ 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`.
/// An IPv6 address representing localhost: `::1`.
///
/// # Examples
///
/// ```
/// #![feature(ip_constructors)]
/// use std::net::Ipv6Addr;
///
/// let addr = Ipv6Addr::localhost();
/// let addr = Ipv6Addr::LOCALHOST;
/// assert_eq!(addr, Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1));
/// ```
#[unstable(feature = "ip_constructors",
reason = "requires greater scrutiny before stabilization",
issue = "44582")]
pub fn localhost() -> Ipv6Addr {
Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1)
}
pub const LOCALHOST: Self = Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1);

/// Creates a new IPv6 address representing the unspecified address: `::`
/// An IPv6 address representing the unspecified address: `::`
///
/// # Examples
///
/// ```
/// #![feature(ip_constructors)]
/// use std::net::Ipv6Addr;
///
/// let addr = Ipv6Addr::unspecified();
/// let addr = Ipv6Addr::UNSPECIFIED;
/// assert_eq!(addr, Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0));
/// ```
#[unstable(feature = "ip_constructors",
reason = "requires greater scrutiny before stabilization",
issue = "44582")]
pub fn unspecified() -> Ipv6Addr {
Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0)
}
pub const UNSPECIFIED: Self = Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0);

/// Returns the eight 16-bit segments that make up this address.
///
Expand Down Expand Up @@ -1414,8 +1428,7 @@ impl From<u128> for Ipv6Addr {
#[stable(feature = "ipv6_from_octets", since = "1.9.0")]
impl From<[u8; 16]> for Ipv6Addr {
fn from(octets: [u8; 16]) -> Ipv6Addr {
let mut inner: c::in6_addr = unsafe { mem::zeroed() };
inner.s6_addr = octets;
let inner = c::in6_addr { s6_addr: octets };
Ipv6Addr::from_inner(inner)
}
}
Expand Down Expand Up @@ -1846,18 +1859,20 @@ mod tests {

#[test]
fn ipv4_from_constructors() {
assert_eq!(Ipv4Addr::localhost(), Ipv4Addr::new(127, 0, 0, 1));
assert!(Ipv4Addr::localhost().is_loopback());
assert_eq!(Ipv4Addr::unspecified(), Ipv4Addr::new(0, 0, 0, 0));
assert!(Ipv4Addr::unspecified().is_unspecified());
assert_eq!(Ipv4Addr::LOCALHOST, Ipv4Addr::new(127, 0, 0, 1));
assert!(Ipv4Addr::LOCALHOST.is_loopback());
assert_eq!(Ipv4Addr::UNSPECIFIED, Ipv4Addr::new(0, 0, 0, 0));
assert!(Ipv4Addr::UNSPECIFIED.is_unspecified());
assert_eq!(Ipv4Addr::BROADCAST, Ipv4Addr::new(255, 255, 255, 255));
assert!(Ipv4Addr::BROADCAST.is_broadcast());
}

#[test]
fn ipv6_from_contructors() {
assert_eq!(Ipv6Addr::localhost(), Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1));
assert!(Ipv6Addr::localhost().is_loopback());
assert_eq!(Ipv6Addr::unspecified(), Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0));
assert!(Ipv6Addr::unspecified().is_unspecified());
assert_eq!(Ipv6Addr::LOCALHOST, Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1));
assert!(Ipv6Addr::LOCALHOST.is_loopback());
assert_eq!(Ipv6Addr::UNSPECIFIED, Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0));
assert!(Ipv6Addr::UNSPECIFIED.is_unspecified());
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion src/libstd/sys/redox/net/netc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ pub struct in_addr {
}

#[derive(Copy, Clone)]
#[repr(align(4))]
#[repr(C)]
pub struct in6_addr {
pub s6_addr: [u8; 16],
__align: [u32; 0],
}

#[derive(Copy, Clone)]
Expand Down
3 changes: 2 additions & 1 deletion src/rustc/libc_shim/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,5 @@ compiler_builtins = { path = "../compiler_builtins_shim" }
# Certain parts of libc are conditionally compiled differently than when used
# outside rustc. See https://github.com/rust-lang/libc/search?l=Rust&q=stdbuild&type=&utf8=%E2%9C%93.
stdbuild = []
default = ["stdbuild"]
default = ["stdbuild", "align"]
align = []

0 comments on commit ebe8df4

Please sign in to comment.