Skip to content

Commit ec0b0df

Browse files
committed
Add a const_sockaddr_setters feature
Unstably add `const` to the `sockaddr_setters` methods. Included API: // core::net impl SocketAddr { pub const fn set_ip(&mut self, new_ip: IpAddr); pub const fn set_port(&mut self, new_port: u16); } impl SocketAddrV4 { pub const fn set_ip(&mut self, new_ip: Ipv4Addr); pub const fn set_port(&mut self, new_port: u16); } impl SocketAddrV6 { pub const fn set_ip(&mut self, new_ip: Ipv6Addr); pub const fn set_port(&mut self, new_port: u16); } Tracking issue: <rust-lang#131714>
1 parent ecb3830 commit ec0b0df

File tree

2 files changed

+25
-16
lines changed

2 files changed

+25
-16
lines changed

core/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@
136136
#![feature(const_raw_ptr_comparison)]
137137
#![feature(const_size_of_val)]
138138
#![feature(const_size_of_val_raw)]
139+
#![feature(const_sockaddr_setters)]
139140
#![feature(const_strict_overflow_ops)]
140141
#![feature(const_swap)]
141142
#![feature(const_try)]

core/src/net/socket_addr.rs

+24-16
Original file line numberDiff line numberDiff line change
@@ -159,9 +159,10 @@ impl SocketAddr {
159159
/// socket.set_ip(IpAddr::V4(Ipv4Addr::new(10, 10, 0, 1)));
160160
/// assert_eq!(socket.ip(), IpAddr::V4(Ipv4Addr::new(10, 10, 0, 1)));
161161
/// ```
162-
#[stable(feature = "sockaddr_setters", since = "1.9.0")]
163162
#[inline]
164-
pub fn set_ip(&mut self, new_ip: IpAddr) {
163+
#[stable(feature = "sockaddr_setters", since = "1.9.0")]
164+
#[rustc_const_unstable(feature = "const_sockaddr_setters", issue = "131714")]
165+
pub const fn set_ip(&mut self, new_ip: IpAddr) {
165166
// `match (*self, new_ip)` would have us mutate a copy of self only to throw it away.
166167
match (self, new_ip) {
167168
(&mut SocketAddr::V4(ref mut a), IpAddr::V4(new_ip)) => a.set_ip(new_ip),
@@ -202,9 +203,10 @@ impl SocketAddr {
202203
/// socket.set_port(1025);
203204
/// assert_eq!(socket.port(), 1025);
204205
/// ```
205-
#[stable(feature = "sockaddr_setters", since = "1.9.0")]
206206
#[inline]
207-
pub fn set_port(&mut self, new_port: u16) {
207+
#[stable(feature = "sockaddr_setters", since = "1.9.0")]
208+
#[rustc_const_unstable(feature = "const_sockaddr_setters", issue = "131714")]
209+
pub const fn set_port(&mut self, new_port: u16) {
208210
match *self {
209211
SocketAddr::V4(ref mut a) => a.set_port(new_port),
210212
SocketAddr::V6(ref mut a) => a.set_port(new_port),
@@ -307,9 +309,10 @@ impl SocketAddrV4 {
307309
/// socket.set_ip(Ipv4Addr::new(192, 168, 0, 1));
308310
/// assert_eq!(socket.ip(), &Ipv4Addr::new(192, 168, 0, 1));
309311
/// ```
310-
#[stable(feature = "sockaddr_setters", since = "1.9.0")]
311312
#[inline]
312-
pub fn set_ip(&mut self, new_ip: Ipv4Addr) {
313+
#[stable(feature = "sockaddr_setters", since = "1.9.0")]
314+
#[rustc_const_unstable(feature = "const_sockaddr_setters", issue = "131714")]
315+
pub const fn set_ip(&mut self, new_ip: Ipv4Addr) {
313316
self.ip = new_ip;
314317
}
315318

@@ -342,9 +345,10 @@ impl SocketAddrV4 {
342345
/// socket.set_port(4242);
343346
/// assert_eq!(socket.port(), 4242);
344347
/// ```
345-
#[stable(feature = "sockaddr_setters", since = "1.9.0")]
346348
#[inline]
347-
pub fn set_port(&mut self, new_port: u16) {
349+
#[stable(feature = "sockaddr_setters", since = "1.9.0")]
350+
#[rustc_const_unstable(feature = "const_sockaddr_setters", issue = "131714")]
351+
pub const fn set_port(&mut self, new_port: u16) {
348352
self.port = new_port;
349353
}
350354
}
@@ -403,9 +407,10 @@ impl SocketAddrV6 {
403407
/// socket.set_ip(Ipv6Addr::new(76, 45, 0, 0, 0, 0, 0, 0));
404408
/// assert_eq!(socket.ip(), &Ipv6Addr::new(76, 45, 0, 0, 0, 0, 0, 0));
405409
/// ```
406-
#[stable(feature = "sockaddr_setters", since = "1.9.0")]
407410
#[inline]
408-
pub fn set_ip(&mut self, new_ip: Ipv6Addr) {
411+
#[stable(feature = "sockaddr_setters", since = "1.9.0")]
412+
#[rustc_const_unstable(feature = "const_sockaddr_setters", issue = "131714")]
413+
pub const fn set_ip(&mut self, new_ip: Ipv6Addr) {
409414
self.ip = new_ip;
410415
}
411416

@@ -438,9 +443,10 @@ impl SocketAddrV6 {
438443
/// socket.set_port(4242);
439444
/// assert_eq!(socket.port(), 4242);
440445
/// ```
441-
#[stable(feature = "sockaddr_setters", since = "1.9.0")]
442446
#[inline]
443-
pub fn set_port(&mut self, new_port: u16) {
447+
#[stable(feature = "sockaddr_setters", since = "1.9.0")]
448+
#[rustc_const_unstable(feature = "const_sockaddr_setters", issue = "131714")]
449+
pub const fn set_port(&mut self, new_port: u16) {
444450
self.port = new_port;
445451
}
446452

@@ -485,9 +491,10 @@ impl SocketAddrV6 {
485491
/// socket.set_flowinfo(56);
486492
/// assert_eq!(socket.flowinfo(), 56);
487493
/// ```
488-
#[stable(feature = "sockaddr_setters", since = "1.9.0")]
489494
#[inline]
490-
pub fn set_flowinfo(&mut self, new_flowinfo: u32) {
495+
#[stable(feature = "sockaddr_setters", since = "1.9.0")]
496+
#[rustc_const_unstable(feature = "const_sockaddr_setters", issue = "131714")]
497+
pub const fn set_flowinfo(&mut self, new_flowinfo: u32) {
491498
self.flowinfo = new_flowinfo;
492499
}
493500

@@ -527,9 +534,10 @@ impl SocketAddrV6 {
527534
/// socket.set_scope_id(42);
528535
/// assert_eq!(socket.scope_id(), 42);
529536
/// ```
530-
#[stable(feature = "sockaddr_setters", since = "1.9.0")]
531537
#[inline]
532-
pub fn set_scope_id(&mut self, new_scope_id: u32) {
538+
#[stable(feature = "sockaddr_setters", since = "1.9.0")]
539+
#[rustc_const_unstable(feature = "const_sockaddr_setters", issue = "131714")]
540+
pub const fn set_scope_id(&mut self, new_scope_id: u32) {
533541
self.scope_id = new_scope_id;
534542
}
535543
}

0 commit comments

Comments
 (0)