Skip to content

Commit ebe8df4

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 ffb09df + c0041f4 commit ebe8df4

File tree

6 files changed

+118
-99
lines changed

6 files changed

+118
-99
lines changed

Diff for: src/Cargo.lock

+43-43
Large diffs are not rendered by default.

Diff for: src/liblibc

Submodule liblibc updated 55 files

Diff for: src/libstd/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,8 @@
252252
#![feature(char_error_internals)]
253253
#![feature(compiler_builtins_lib)]
254254
#![feature(const_fn)]
255+
#![feature(const_int_ops)]
256+
#![feature(const_ip)]
255257
#![feature(core_intrinsics)]
256258
#![feature(dropck_eyepatch)]
257259
#![feature(exact_size_is_empty)]
@@ -281,6 +283,7 @@
281283
#![feature(ptr_internals)]
282284
#![feature(raw)]
283285
#![feature(rustc_attrs)]
286+
#![feature(rustc_const_unstable)]
284287
#![feature(std_internals)]
285288
#![feature(stdsimd)]
286289
#![feature(shrink_to)]

Diff for: src/libstd/net/ip.rs

+68-53
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
use cmp::Ordering;
1717
use fmt;
1818
use hash;
19-
use mem;
20-
use net::{hton, ntoh};
2119
use sys::net::netc as c;
2220
use sys_common::{AsInner, FromInner};
2321

@@ -340,52 +338,67 @@ impl Ipv4Addr {
340338
/// let addr = Ipv4Addr::new(127, 0, 0, 1);
341339
/// ```
342340
#[stable(feature = "rust1", since = "1.0.0")]
343-
pub fn new(a: u8, b: u8, c: u8, d: u8) -> Ipv4Addr {
341+
#[rustc_const_unstable(feature = "const_ip")]
342+
pub const fn new(a: u8, b: u8, c: u8, d: u8) -> Ipv4Addr {
344343
Ipv4Addr {
345344
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)),
345+
s_addr: u32::to_be(
346+
((a as u32) << 24) |
347+
((b as u32) << 16) |
348+
((c as u32) << 8) |
349+
(d as u32)
350+
),
350351
}
351352
}
352353
}
353354

354-
/// Creates a new IPv4 address with the address pointing to localhost: 127.0.0.1.
355+
/// An IPv4 address with the address pointing to localhost: 127.0.0.1.
355356
///
356357
/// # Examples
357358
///
358359
/// ```
359360
/// #![feature(ip_constructors)]
360361
/// use std::net::Ipv4Addr;
361362
///
362-
/// let addr = Ipv4Addr::localhost();
363+
/// let addr = Ipv4Addr::LOCALHOST;
363364
/// assert_eq!(addr, Ipv4Addr::new(127, 0, 0, 1));
364365
/// ```
365366
#[unstable(feature = "ip_constructors",
366367
reason = "requires greater scrutiny before stabilization",
367368
issue = "44582")]
368-
pub fn localhost() -> Ipv4Addr {
369-
Ipv4Addr::new(127, 0, 0, 1)
370-
}
369+
pub const LOCALHOST: Self = Ipv4Addr::new(127, 0, 0, 1);
371370

372-
/// Creates a new IPv4 address representing an unspecified address: 0.0.0.0
371+
/// An IPv4 address representing an unspecified address: 0.0.0.0
373372
///
374373
/// # Examples
375374
///
376375
/// ```
377376
/// #![feature(ip_constructors)]
378377
/// use std::net::Ipv4Addr;
379378
///
380-
/// let addr = Ipv4Addr::unspecified();
379+
/// let addr = Ipv4Addr::UNSPECIFIED;
381380
/// assert_eq!(addr, Ipv4Addr::new(0, 0, 0, 0));
382381
/// ```
383382
#[unstable(feature = "ip_constructors",
384383
reason = "requires greater scrutiny before stabilization",
385384
issue = "44582")]
386-
pub fn unspecified() -> Ipv4Addr {
387-
Ipv4Addr::new(0, 0, 0, 0)
388-
}
385+
pub const UNSPECIFIED: Self = Ipv4Addr::new(0, 0, 0, 0);
386+
387+
/// An IPv4 address representing the broadcast address: 255.255.255.255
388+
///
389+
/// # Examples
390+
///
391+
/// ```
392+
/// #![feature(ip_constructors)]
393+
/// use std::net::Ipv4Addr;
394+
///
395+
/// let addr = Ipv4Addr::BROADCAST;
396+
/// assert_eq!(addr, Ipv4Addr::new(255, 255, 255, 255));
397+
/// ```
398+
#[unstable(feature = "ip_constructors",
399+
reason = "requires greater scrutiny before stabilization",
400+
issue = "44582")]
401+
pub const BROADCAST: Self = Ipv4Addr::new(255, 255, 255, 255);
389402

390403
/// Returns the four eight-bit integers that make up this address.
391404
///
@@ -399,7 +412,7 @@ impl Ipv4Addr {
399412
/// ```
400413
#[stable(feature = "rust1", since = "1.0.0")]
401414
pub fn octets(&self) -> [u8; 4] {
402-
let bits = ntoh(self.inner.s_addr);
415+
let bits = u32::from_be(self.inner.s_addr);
403416
[(bits >> 24) as u8, (bits >> 16) as u8, (bits >> 8) as u8, bits as u8]
404417
}
405418

@@ -573,8 +586,7 @@ impl Ipv4Addr {
573586
/// ```
574587
#[stable(since = "1.7.0", feature = "ip_17")]
575588
pub fn is_broadcast(&self) -> bool {
576-
self.octets()[0] == 255 && self.octets()[1] == 255 &&
577-
self.octets()[2] == 255 && self.octets()[3] == 255
589+
self == &Self::BROADCAST
578590
}
579591

580592
/// Returns [`true`] if this address is in a range designated for documentation.
@@ -763,7 +775,7 @@ impl PartialOrd<IpAddr> for Ipv4Addr {
763775
#[stable(feature = "rust1", since = "1.0.0")]
764776
impl Ord for Ipv4Addr {
765777
fn cmp(&self, other: &Ipv4Addr) -> Ordering {
766-
ntoh(self.inner.s_addr).cmp(&ntoh(other.inner.s_addr))
778+
u32::from_be(self.inner.s_addr).cmp(&u32::from_be(other.inner.s_addr))
767779
}
768780
}
769781

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

873-
/// Creates a new IPv6 address representing localhost: `::1`.
891+
/// An IPv6 address representing localhost: `::1`.
874892
///
875893
/// # Examples
876894
///
877895
/// ```
878896
/// #![feature(ip_constructors)]
879897
/// use std::net::Ipv6Addr;
880898
///
881-
/// let addr = Ipv6Addr::localhost();
899+
/// let addr = Ipv6Addr::LOCALHOST;
882900
/// assert_eq!(addr, Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1));
883901
/// ```
884902
#[unstable(feature = "ip_constructors",
885903
reason = "requires greater scrutiny before stabilization",
886904
issue = "44582")]
887-
pub fn localhost() -> Ipv6Addr {
888-
Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1)
889-
}
905+
pub const LOCALHOST: Self = Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1);
890906

891-
/// Creates a new IPv6 address representing the unspecified address: `::`
907+
/// An IPv6 address representing the unspecified address: `::`
892908
///
893909
/// # Examples
894910
///
895911
/// ```
896912
/// #![feature(ip_constructors)]
897913
/// use std::net::Ipv6Addr;
898914
///
899-
/// let addr = Ipv6Addr::unspecified();
915+
/// let addr = Ipv6Addr::UNSPECIFIED;
900916
/// assert_eq!(addr, Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0));
901917
/// ```
902918
#[unstable(feature = "ip_constructors",
903919
reason = "requires greater scrutiny before stabilization",
904920
issue = "44582")]
905-
pub fn unspecified() -> Ipv6Addr {
906-
Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0)
907-
}
921+
pub const UNSPECIFIED: Self = Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0);
908922

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

18471860
#[test]
18481861
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());
1862+
assert_eq!(Ipv4Addr::LOCALHOST, Ipv4Addr::new(127, 0, 0, 1));
1863+
assert!(Ipv4Addr::LOCALHOST.is_loopback());
1864+
assert_eq!(Ipv4Addr::UNSPECIFIED, Ipv4Addr::new(0, 0, 0, 0));
1865+
assert!(Ipv4Addr::UNSPECIFIED.is_unspecified());
1866+
assert_eq!(Ipv4Addr::BROADCAST, Ipv4Addr::new(255, 255, 255, 255));
1867+
assert!(Ipv4Addr::BROADCAST.is_broadcast());
18531868
}
18541869

18551870
#[test]
18561871
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());
1872+
assert_eq!(Ipv6Addr::LOCALHOST, Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1));
1873+
assert!(Ipv6Addr::LOCALHOST.is_loopback());
1874+
assert_eq!(Ipv6Addr::UNSPECIFIED, Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0));
1875+
assert!(Ipv6Addr::UNSPECIFIED.is_unspecified());
18611876
}
18621877

18631878
#[test]

Diff for: src/libstd/sys/redox/net/netc.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ pub struct in_addr {
2424
}
2525

2626
#[derive(Copy, Clone)]
27+
#[repr(align(4))]
2728
#[repr(C)]
2829
pub struct in6_addr {
2930
pub s6_addr: [u8; 16],
30-
__align: [u32; 0],
3131
}
3232

3333
#[derive(Copy, Clone)]

Diff for: src/rustc/libc_shim/Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,5 @@ compiler_builtins = { path = "../compiler_builtins_shim" }
3636
# Certain parts of libc are conditionally compiled differently than when used
3737
# outside rustc. See https://github.com/rust-lang/libc/search?l=Rust&q=stdbuild&type=&utf8=%E2%9C%93.
3838
stdbuild = []
39-
default = ["stdbuild"]
39+
default = ["stdbuild", "align"]
40+
align = []

0 commit comments

Comments
 (0)