Skip to content

Commit 5b84b9a

Browse files
committed
Constify methods of std::net::SocketAddr, SocketAddrV4 and SocketAddrV6
The following methods are made unstable const under the `const_socketaddr` feature: `SocketAddr` - `ip` - `port` - `is_ipv4` - `is_ipv6` `SocketAddrV4` - `ip` - `port` `SocketAddrV6` - `ip` - `port` - `flowinfo` - `scope_id`
1 parent d1206f9 commit 5b84b9a

File tree

2 files changed

+21
-10
lines changed

2 files changed

+21
-10
lines changed

library/std/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,7 @@
247247
#![feature(const_ip)]
248248
#![feature(const_ipv6)]
249249
#![feature(const_raw_ptr_deref)]
250+
#![feature(const_socketaddr)]
250251
#![feature(const_ipv4)]
251252
#![feature(container_error_extra)]
252253
#![feature(core_intrinsics)]

library/std/src/net/addr.rs

+20-10
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,8 @@ impl SocketAddr {
143143
/// assert_eq!(socket.ip(), IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)));
144144
/// ```
145145
#[stable(feature = "ip_addr", since = "1.7.0")]
146-
pub fn ip(&self) -> IpAddr {
146+
#[rustc_const_unstable(feature = "const_socketaddr", issue = "82485")]
147+
pub const fn ip(&self) -> IpAddr {
147148
match *self {
148149
SocketAddr::V4(ref a) => IpAddr::V4(*a.ip()),
149150
SocketAddr::V6(ref a) => IpAddr::V6(*a.ip()),
@@ -182,7 +183,8 @@ impl SocketAddr {
182183
/// assert_eq!(socket.port(), 8080);
183184
/// ```
184185
#[stable(feature = "rust1", since = "1.0.0")]
185-
pub fn port(&self) -> u16 {
186+
#[rustc_const_unstable(feature = "const_socketaddr", issue = "82485")]
187+
pub const fn port(&self) -> u16 {
186188
match *self {
187189
SocketAddr::V4(ref a) => a.port(),
188190
SocketAddr::V6(ref a) => a.port(),
@@ -224,7 +226,8 @@ impl SocketAddr {
224226
/// assert_eq!(socket.is_ipv6(), false);
225227
/// ```
226228
#[stable(feature = "sockaddr_checker", since = "1.16.0")]
227-
pub fn is_ipv4(&self) -> bool {
229+
#[rustc_const_unstable(feature = "const_socketaddr", issue = "82485")]
230+
pub const fn is_ipv4(&self) -> bool {
228231
matches!(*self, SocketAddr::V4(_))
229232
}
230233

@@ -244,7 +247,8 @@ impl SocketAddr {
244247
/// assert_eq!(socket.is_ipv6(), true);
245248
/// ```
246249
#[stable(feature = "sockaddr_checker", since = "1.16.0")]
247-
pub fn is_ipv6(&self) -> bool {
250+
#[rustc_const_unstable(feature = "const_socketaddr", issue = "82485")]
251+
pub const fn is_ipv6(&self) -> bool {
248252
matches!(*self, SocketAddr::V6(_))
249253
}
250254
}
@@ -284,7 +288,8 @@ impl SocketAddrV4 {
284288
/// assert_eq!(socket.ip(), &Ipv4Addr::new(127, 0, 0, 1));
285289
/// ```
286290
#[stable(feature = "rust1", since = "1.0.0")]
287-
pub fn ip(&self) -> &Ipv4Addr {
291+
#[rustc_const_unstable(feature = "const_socketaddr", issue = "82485")]
292+
pub const fn ip(&self) -> &Ipv4Addr {
288293
// SAFETY: `Ipv4Addr` is `#[repr(C)] struct { _: in_addr; }`.
289294
// It is safe to cast from `&in_addr` to `&Ipv4Addr`.
290295
unsafe { &*(&self.inner.sin_addr as *const c::in_addr as *const Ipv4Addr) }
@@ -317,7 +322,8 @@ impl SocketAddrV4 {
317322
/// assert_eq!(socket.port(), 8080);
318323
/// ```
319324
#[stable(feature = "rust1", since = "1.0.0")]
320-
pub fn port(&self) -> u16 {
325+
#[rustc_const_unstable(feature = "const_socketaddr", issue = "82485")]
326+
pub const fn port(&self) -> u16 {
321327
ntohs(self.inner.sin_port)
322328
}
323329

@@ -380,7 +386,8 @@ impl SocketAddrV6 {
380386
/// assert_eq!(socket.ip(), &Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1));
381387
/// ```
382388
#[stable(feature = "rust1", since = "1.0.0")]
383-
pub fn ip(&self) -> &Ipv6Addr {
389+
#[rustc_const_unstable(feature = "const_socketaddr", issue = "82485")]
390+
pub const fn ip(&self) -> &Ipv6Addr {
384391
unsafe { &*(&self.inner.sin6_addr as *const c::in6_addr as *const Ipv6Addr) }
385392
}
386393

@@ -411,7 +418,8 @@ impl SocketAddrV6 {
411418
/// assert_eq!(socket.port(), 8080);
412419
/// ```
413420
#[stable(feature = "rust1", since = "1.0.0")]
414-
pub fn port(&self) -> u16 {
421+
#[rustc_const_unstable(feature = "const_socketaddr", issue = "82485")]
422+
pub const fn port(&self) -> u16 {
415423
ntohs(self.inner.sin6_port)
416424
}
417425

@@ -452,7 +460,8 @@ impl SocketAddrV6 {
452460
/// assert_eq!(socket.flowinfo(), 10);
453461
/// ```
454462
#[stable(feature = "rust1", since = "1.0.0")]
455-
pub fn flowinfo(&self) -> u32 {
463+
#[rustc_const_unstable(feature = "const_socketaddr", issue = "82485")]
464+
pub const fn flowinfo(&self) -> u32 {
456465
self.inner.sin6_flowinfo
457466
}
458467

@@ -490,7 +499,8 @@ impl SocketAddrV6 {
490499
/// assert_eq!(socket.scope_id(), 78);
491500
/// ```
492501
#[stable(feature = "rust1", since = "1.0.0")]
493-
pub fn scope_id(&self) -> u32 {
502+
#[rustc_const_unstable(feature = "const_socketaddr", issue = "82485")]
503+
pub const fn scope_id(&self) -> u32 {
494504
self.inner.sin6_scope_id
495505
}
496506

0 commit comments

Comments
 (0)