Skip to content

Commit

Permalink
Rollup merge of rust-lang#52872 - faern:use-modern-alignment-libc, r=…
Browse files Browse the repository at this point in the history
…TimNN

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

Implements/fixes rust-lang#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
cramertj authored Aug 3, 2018
2 parents 7e031b0 + 312cdb4 commit a233ec8
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 52 deletions.
2 changes: 1 addition & 1 deletion src/liblibc
Submodule liblibc updated 48 files
+1 −1 Cargo.lock
+1 −0 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
+10 −2 ci/run.sh
+1 −0 libc-test/Cargo.toml
+9 −9 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
+4 −0 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
+4 −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
+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
2 changes: 1 addition & 1 deletion src/libstd/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ alloc_system = { path = "../liballoc_system" }
panic_unwind = { path = "../libpanic_unwind", optional = true }
panic_abort = { path = "../libpanic_abort" }
core = { path = "../libcore" }
libc = { path = "../rustc/libc_shim" }
libc = { path = "../rustc/libc_shim", features = ["align"] }
compiler_builtins = { path = "../rustc/compiler_builtins_shim" }
profiler_builtins = { path = "../libprofiler_builtins", optional = true }
unwind = { path = "../libunwind" }
Expand Down
3 changes: 3 additions & 0 deletions src/libstd/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,8 @@
#![feature(collections_range)]
#![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 @@ -292,6 +294,7 @@
#![feature(rand)]
#![feature(raw)]
#![feature(rustc_attrs)]
#![feature(rustc_const_unstable)]
#![feature(std_internals)]
#![feature(stdsimd)]
#![feature(shrink_to)]
Expand Down
117 changes: 67 additions & 50 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,52 +339,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 +413,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 +587,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 +776,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 +869,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 @@ -1846,18 +1861,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
1 change: 1 addition & 0 deletions src/rustc/libc_shim/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,4 @@ compiler_builtins = { path = "../compiler_builtins_shim" }
# outside rustc. See https://github.com/rust-lang/libc/search?l=Rust&q=stdbuild&type=&utf8=%E2%9C%93.
stdbuild = []
default = ["stdbuild"]
align = []

0 comments on commit a233ec8

Please sign in to comment.