Skip to content

Commit facbc12

Browse files
committed
Auto merge of #52872 - faern:use-modern-alignment-libc, r=TimNN
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.
2 parents 3edb355 + 312cdb4 commit facbc12

File tree

5 files changed

+73
-52
lines changed

5 files changed

+73
-52
lines changed

src/liblibc

Submodule liblibc updated 48 files

src/libstd/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ alloc_system = { path = "../liballoc_system" }
1919
panic_unwind = { path = "../libpanic_unwind", optional = true }
2020
panic_abort = { path = "../libpanic_abort" }
2121
core = { path = "../libcore" }
22-
libc = { path = "../rustc/libc_shim" }
22+
libc = { path = "../rustc/libc_shim", features = ["align"] }
2323
compiler_builtins = { path = "../rustc/compiler_builtins_shim" }
2424
profiler_builtins = { path = "../libprofiler_builtins", optional = true }
2525
unwind = { path = "../libunwind" }

src/libstd/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,8 @@
254254
#![feature(collections_range)]
255255
#![feature(compiler_builtins_lib)]
256256
#![feature(const_fn)]
257+
#![feature(const_int_ops)]
258+
#![feature(const_ip)]
257259
#![feature(core_intrinsics)]
258260
#![feature(dropck_eyepatch)]
259261
#![feature(exact_size_is_empty)]
@@ -292,6 +294,7 @@
292294
#![feature(rand)]
293295
#![feature(raw)]
294296
#![feature(rustc_attrs)]
297+
#![feature(rustc_const_unstable)]
295298
#![feature(std_internals)]
296299
#![feature(stdsimd)]
297300
#![feature(shrink_to)]

src/libstd/net/ip.rs

+67-50
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,52 +339,67 @@ 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
}
353355

354-
/// Creates a new IPv4 address with the address pointing to localhost: 127.0.0.1.
356+
/// An IPv4 address with the address pointing to localhost: 127.0.0.1.
355357
///
356358
/// # Examples
357359
///
358360
/// ```
359361
/// #![feature(ip_constructors)]
360362
/// use std::net::Ipv4Addr;
361363
///
362-
/// let addr = Ipv4Addr::localhost();
364+
/// let addr = Ipv4Addr::LOCALHOST;
363365
/// assert_eq!(addr, Ipv4Addr::new(127, 0, 0, 1));
364366
/// ```
365367
#[unstable(feature = "ip_constructors",
366368
reason = "requires greater scrutiny before stabilization",
367369
issue = "44582")]
368-
pub fn localhost() -> Ipv4Addr {
369-
Ipv4Addr::new(127, 0, 0, 1)
370-
}
370+
pub const LOCALHOST: Self = Ipv4Addr::new(127, 0, 0, 1);
371371

372-
/// Creates a new IPv4 address representing an unspecified address: 0.0.0.0
372+
/// An IPv4 address representing an unspecified address: 0.0.0.0
373373
///
374374
/// # Examples
375375
///
376376
/// ```
377377
/// #![feature(ip_constructors)]
378378
/// use std::net::Ipv4Addr;
379379
///
380-
/// let addr = Ipv4Addr::unspecified();
380+
/// let addr = Ipv4Addr::UNSPECIFIED;
381381
/// assert_eq!(addr, Ipv4Addr::new(0, 0, 0, 0));
382382
/// ```
383383
#[unstable(feature = "ip_constructors",
384384
reason = "requires greater scrutiny before stabilization",
385385
issue = "44582")]
386-
pub fn unspecified() -> Ipv4Addr {
387-
Ipv4Addr::new(0, 0, 0, 0)
388-
}
386+
pub const UNSPECIFIED: Self = Ipv4Addr::new(0, 0, 0, 0);
387+
388+
/// An IPv4 address representing the broadcast address: 255.255.255.255
389+
///
390+
/// # Examples
391+
///
392+
/// ```
393+
/// #![feature(ip_constructors)]
394+
/// use std::net::Ipv4Addr;
395+
///
396+
/// let addr = Ipv4Addr::BROADCAST;
397+
/// assert_eq!(addr, Ipv4Addr::new(255, 255, 255, 255));
398+
/// ```
399+
#[unstable(feature = "ip_constructors",
400+
reason = "requires greater scrutiny before stabilization",
401+
issue = "44582")]
402+
pub const BROADCAST: Self = Ipv4Addr::new(255, 255, 255, 255);
389403

390404
/// Returns the four eight-bit integers that make up this address.
391405
///
@@ -399,7 +413,7 @@ impl Ipv4Addr {
399413
/// ```
400414
#[stable(feature = "rust1", since = "1.0.0")]
401415
pub fn octets(&self) -> [u8; 4] {
402-
let bits = ntoh(self.inner.s_addr);
416+
let bits = u32::from_be(self.inner.s_addr);
403417
[(bits >> 24) as u8, (bits >> 16) as u8, (bits >> 8) as u8, bits as u8]
404418
}
405419

@@ -573,8 +587,7 @@ impl Ipv4Addr {
573587
/// ```
574588
#[stable(since = "1.7.0", feature = "ip_17")]
575589
pub fn is_broadcast(&self) -> bool {
576-
self.octets()[0] == 255 && self.octets()[1] == 255 &&
577-
self.octets()[2] == 255 && self.octets()[3] == 255
590+
self == &Self::BROADCAST
578591
}
579592

580593
/// Returns [`true`] if this address is in a range designated for documentation.
@@ -763,7 +776,7 @@ impl PartialOrd<IpAddr> for Ipv4Addr {
763776
#[stable(feature = "rust1", since = "1.0.0")]
764777
impl Ord for Ipv4Addr {
765778
fn cmp(&self, other: &Ipv4Addr) -> Ordering {
766-
ntoh(self.inner.s_addr).cmp(&ntoh(other.inner.s_addr))
779+
u32::from_be(self.inner.s_addr).cmp(&u32::from_be(other.inner.s_addr))
767780
}
768781
}
769782

@@ -856,55 +869,57 @@ impl Ipv6Addr {
856869
/// let addr = Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff);
857870
/// ```
858871
#[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 }
872+
#[rustc_const_unstable(feature = "const_ip")]
873+
pub const fn new(a: u16, b: u16, c: u16, d: u16, e: u16, f: u16,
874+
g: u16, h: u16) -> Ipv6Addr {
875+
Ipv6Addr {
876+
inner: c::in6_addr {
877+
s6_addr: [
878+
(a >> 8) as u8, a as u8,
879+
(b >> 8) as u8, b as u8,
880+
(c >> 8) as u8, c as u8,
881+
(d >> 8) as u8, d as u8,
882+
(e >> 8) as u8, e as u8,
883+
(f >> 8) as u8, f as u8,
884+
(g >> 8) as u8, g as u8,
885+
(h >> 8) as u8, h as u8
886+
],
887+
}
888+
}
889+
871890
}
872891

873-
/// Creates a new IPv6 address representing localhost: `::1`.
892+
/// An IPv6 address representing localhost: `::1`.
874893
///
875894
/// # Examples
876895
///
877896
/// ```
878897
/// #![feature(ip_constructors)]
879898
/// use std::net::Ipv6Addr;
880899
///
881-
/// let addr = Ipv6Addr::localhost();
900+
/// let addr = Ipv6Addr::LOCALHOST;
882901
/// assert_eq!(addr, Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1));
883902
/// ```
884903
#[unstable(feature = "ip_constructors",
885904
reason = "requires greater scrutiny before stabilization",
886905
issue = "44582")]
887-
pub fn localhost() -> Ipv6Addr {
888-
Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1)
889-
}
906+
pub const LOCALHOST: Self = Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1);
890907

891-
/// Creates a new IPv6 address representing the unspecified address: `::`
908+
/// An IPv6 address representing the unspecified address: `::`
892909
///
893910
/// # Examples
894911
///
895912
/// ```
896913
/// #![feature(ip_constructors)]
897914
/// use std::net::Ipv6Addr;
898915
///
899-
/// let addr = Ipv6Addr::unspecified();
916+
/// let addr = Ipv6Addr::UNSPECIFIED;
900917
/// assert_eq!(addr, Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0));
901918
/// ```
902919
#[unstable(feature = "ip_constructors",
903920
reason = "requires greater scrutiny before stabilization",
904921
issue = "44582")]
905-
pub fn unspecified() -> Ipv6Addr {
906-
Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0)
907-
}
922+
pub const UNSPECIFIED: Self = Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0);
908923

909924
/// Returns the eight 16-bit segments that make up this address.
910925
///
@@ -1846,18 +1861,20 @@ mod tests {
18461861

18471862
#[test]
18481863
fn ipv4_from_constructors() {
1849-
assert_eq!(Ipv4Addr::localhost(), Ipv4Addr::new(127, 0, 0, 1));
1850-
assert!(Ipv4Addr::localhost().is_loopback());
1851-
assert_eq!(Ipv4Addr::unspecified(), Ipv4Addr::new(0, 0, 0, 0));
1852-
assert!(Ipv4Addr::unspecified().is_unspecified());
1864+
assert_eq!(Ipv4Addr::LOCALHOST, Ipv4Addr::new(127, 0, 0, 1));
1865+
assert!(Ipv4Addr::LOCALHOST.is_loopback());
1866+
assert_eq!(Ipv4Addr::UNSPECIFIED, Ipv4Addr::new(0, 0, 0, 0));
1867+
assert!(Ipv4Addr::UNSPECIFIED.is_unspecified());
1868+
assert_eq!(Ipv4Addr::BROADCAST, Ipv4Addr::new(255, 255, 255, 255));
1869+
assert!(Ipv4Addr::BROADCAST.is_broadcast());
18531870
}
18541871

18551872
#[test]
18561873
fn ipv6_from_contructors() {
1857-
assert_eq!(Ipv6Addr::localhost(), Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1));
1858-
assert!(Ipv6Addr::localhost().is_loopback());
1859-
assert_eq!(Ipv6Addr::unspecified(), Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0));
1860-
assert!(Ipv6Addr::unspecified().is_unspecified());
1874+
assert_eq!(Ipv6Addr::LOCALHOST, Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1));
1875+
assert!(Ipv6Addr::LOCALHOST.is_loopback());
1876+
assert_eq!(Ipv6Addr::UNSPECIFIED, Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0));
1877+
assert!(Ipv6Addr::UNSPECIFIED.is_unspecified());
18611878
}
18621879

18631880
#[test]

src/rustc/libc_shim/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,4 @@ compiler_builtins = { path = "../compiler_builtins_shim" }
3737
# outside rustc. See https://github.com/rust-lang/libc/search?l=Rust&q=stdbuild&type=&utf8=%E2%9C%93.
3838
stdbuild = []
3939
default = ["stdbuild"]
40+
align = []

0 commit comments

Comments
 (0)