Skip to content

Commit 8ab4ce2

Browse files
authored
Rollup merge of rust-lang#82487 - CDirkx:const-socketaddr, r=m-ou-se
Constify methods of `std::net::SocketAddr`, `SocketAddrV4` and `SocketAddrV6` The following methods are made unstable const under the `const_socketaddr` feature (rust-lang#82485): ```rust // std::net impl SocketAddr { pub const fn ip(&self) -> IpAddr; pub const fn port(&self) -> u16; pub const fn is_ipv4(&self) -> bool; pub const fn is_ipv6(&self) -> bool; } impl SocketAddrV4 { pub const fn ip(&self) -> IpAddr; pub const fn port(&self) -> u16; } impl SocketAddrV6 { pub const fn ip(&self) -> IpAddr; pub const fn port(&self) -> u16; pub const fn flowinfo(&self) -> u32; pub const fn scope_id(&self) -> u32; } ``` Note: `SocketAddrV4::ip` and `SocketAddrV6::ip` use pointer casting and depend on the unstable feature `const_raw_ptr_deref`
2 parents 629976b + 08200ed commit 8ab4ce2

File tree

2 files changed

+21
-10
lines changed

2 files changed

+21
-10
lines changed

std/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@
249249
#![feature(const_ip)]
250250
#![feature(const_ipv6)]
251251
#![feature(const_raw_ptr_deref)]
252+
#![feature(const_socketaddr)]
252253
#![feature(const_ipv4)]
253254
#![feature(container_error_extra)]
254255
#![feature(core_intrinsics)]

std/src/net/addr.rs

+20-10
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,8 @@ impl SocketAddr {
149149
/// assert_eq!(socket.ip(), IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)));
150150
/// ```
151151
#[stable(feature = "ip_addr", since = "1.7.0")]
152-
pub fn ip(&self) -> IpAddr {
152+
#[rustc_const_unstable(feature = "const_socketaddr", issue = "82485")]
153+
pub const fn ip(&self) -> IpAddr {
153154
match *self {
154155
SocketAddr::V4(ref a) => IpAddr::V4(*a.ip()),
155156
SocketAddr::V6(ref a) => IpAddr::V6(*a.ip()),
@@ -188,7 +189,8 @@ impl SocketAddr {
188189
/// assert_eq!(socket.port(), 8080);
189190
/// ```
190191
#[stable(feature = "rust1", since = "1.0.0")]
191-
pub fn port(&self) -> u16 {
192+
#[rustc_const_unstable(feature = "const_socketaddr", issue = "82485")]
193+
pub const fn port(&self) -> u16 {
192194
match *self {
193195
SocketAddr::V4(ref a) => a.port(),
194196
SocketAddr::V6(ref a) => a.port(),
@@ -230,7 +232,8 @@ impl SocketAddr {
230232
/// assert_eq!(socket.is_ipv6(), false);
231233
/// ```
232234
#[stable(feature = "sockaddr_checker", since = "1.16.0")]
233-
pub fn is_ipv4(&self) -> bool {
235+
#[rustc_const_unstable(feature = "const_socketaddr", issue = "82485")]
236+
pub const fn is_ipv4(&self) -> bool {
234237
matches!(*self, SocketAddr::V4(_))
235238
}
236239

@@ -250,7 +253,8 @@ impl SocketAddr {
250253
/// assert_eq!(socket.is_ipv6(), true);
251254
/// ```
252255
#[stable(feature = "sockaddr_checker", since = "1.16.0")]
253-
pub fn is_ipv6(&self) -> bool {
256+
#[rustc_const_unstable(feature = "const_socketaddr", issue = "82485")]
257+
pub const fn is_ipv6(&self) -> bool {
254258
matches!(*self, SocketAddr::V6(_))
255259
}
256260
}
@@ -290,7 +294,8 @@ impl SocketAddrV4 {
290294
/// assert_eq!(socket.ip(), &Ipv4Addr::new(127, 0, 0, 1));
291295
/// ```
292296
#[stable(feature = "rust1", since = "1.0.0")]
293-
pub fn ip(&self) -> &Ipv4Addr {
297+
#[rustc_const_unstable(feature = "const_socketaddr", issue = "82485")]
298+
pub const fn ip(&self) -> &Ipv4Addr {
294299
// SAFETY: `Ipv4Addr` is `#[repr(C)] struct { _: in_addr; }`.
295300
// It is safe to cast from `&in_addr` to `&Ipv4Addr`.
296301
unsafe { &*(&self.inner.sin_addr as *const c::in_addr as *const Ipv4Addr) }
@@ -323,7 +328,8 @@ impl SocketAddrV4 {
323328
/// assert_eq!(socket.port(), 8080);
324329
/// ```
325330
#[stable(feature = "rust1", since = "1.0.0")]
326-
pub fn port(&self) -> u16 {
331+
#[rustc_const_unstable(feature = "const_socketaddr", issue = "82485")]
332+
pub const fn port(&self) -> u16 {
327333
ntohs(self.inner.sin_port)
328334
}
329335

@@ -386,7 +392,8 @@ impl SocketAddrV6 {
386392
/// assert_eq!(socket.ip(), &Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1));
387393
/// ```
388394
#[stable(feature = "rust1", since = "1.0.0")]
389-
pub fn ip(&self) -> &Ipv6Addr {
395+
#[rustc_const_unstable(feature = "const_socketaddr", issue = "82485")]
396+
pub const fn ip(&self) -> &Ipv6Addr {
390397
unsafe { &*(&self.inner.sin6_addr as *const c::in6_addr as *const Ipv6Addr) }
391398
}
392399

@@ -417,7 +424,8 @@ impl SocketAddrV6 {
417424
/// assert_eq!(socket.port(), 8080);
418425
/// ```
419426
#[stable(feature = "rust1", since = "1.0.0")]
420-
pub fn port(&self) -> u16 {
427+
#[rustc_const_unstable(feature = "const_socketaddr", issue = "82485")]
428+
pub const fn port(&self) -> u16 {
421429
ntohs(self.inner.sin6_port)
422430
}
423431

@@ -458,7 +466,8 @@ impl SocketAddrV6 {
458466
/// assert_eq!(socket.flowinfo(), 10);
459467
/// ```
460468
#[stable(feature = "rust1", since = "1.0.0")]
461-
pub fn flowinfo(&self) -> u32 {
469+
#[rustc_const_unstable(feature = "const_socketaddr", issue = "82485")]
470+
pub const fn flowinfo(&self) -> u32 {
462471
self.inner.sin6_flowinfo
463472
}
464473

@@ -496,7 +505,8 @@ impl SocketAddrV6 {
496505
/// assert_eq!(socket.scope_id(), 78);
497506
/// ```
498507
#[stable(feature = "rust1", since = "1.0.0")]
499-
pub fn scope_id(&self) -> u32 {
508+
#[rustc_const_unstable(feature = "const_socketaddr", issue = "82485")]
509+
pub const fn scope_id(&self) -> u32 {
500510
self.inner.sin6_scope_id
501511
}
502512

0 commit comments

Comments
 (0)