@@ -67,6 +67,17 @@ impl SocketAddr {
67
67
}
68
68
}
69
69
70
+ /// Change the IP address associated with this socket address.
71
+ #[ unstable( feature = "sockaddr_setters" , reason = "recent addition" , issue = "31572" ) ]
72
+ pub fn set_ip ( & mut self , new_ip : IpAddr ) {
73
+ // `match (*self, new_ip)` would have us mutate a copy of self only to throw it away.
74
+ match ( self , new_ip) {
75
+ ( & mut SocketAddr :: V4 ( ref mut a) , IpAddr :: V4 ( new_ip) ) => a. set_ip ( new_ip) ,
76
+ ( & mut SocketAddr :: V6 ( ref mut a) , IpAddr :: V6 ( new_ip) ) => a. set_ip ( new_ip) ,
77
+ ( self_, new_ip) => * self_ = Self :: new ( new_ip, self_. port ( ) ) ,
78
+ }
79
+ }
80
+
70
81
/// Returns the port number associated with this socket address.
71
82
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
72
83
pub fn port ( & self ) -> u16 {
@@ -77,7 +88,7 @@ impl SocketAddr {
77
88
}
78
89
79
90
/// Change the port number associated with this socket address.
80
- #[ unstable( feature = "sockaddr_set_port " , reason = "recent addition" , issue = "0 " ) ] // FIXME add tracking issue
91
+ #[ unstable( feature = "sockaddr_setters " , reason = "recent addition" , issue = "31572 " ) ]
81
92
pub fn set_port ( & mut self , new_port : u16 ) {
82
93
match * self {
83
94
SocketAddr :: V4 ( ref mut a) => a. set_port ( new_port) ,
@@ -108,12 +119,16 @@ impl SocketAddrV4 {
108
119
}
109
120
}
110
121
122
+ /// Change the IP address associated with this socket address.
123
+ #[ unstable( feature = "sockaddr_setters" , reason = "recent addition" , issue = "31572" ) ]
124
+ pub fn set_ip ( & mut self , new_ip : Ipv4Addr ) { self . inner . sin_addr = * new_ip. as_inner ( ) }
125
+
111
126
/// Returns the port number associated with this socket address.
112
127
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
113
128
pub fn port ( & self ) -> u16 { ntoh ( self . inner . sin_port ) }
114
129
115
130
/// Change the port number associated with this socket address.
116
- #[ unstable( feature = "sockaddr_set_port " , reason = "recent addition" , issue = "0 " ) ] // FIXME add tracking issue
131
+ #[ unstable( feature = "sockaddr_setters " , reason = "recent addition" , issue = "31572 " ) ]
117
132
pub fn set_port ( & mut self , new_port : u16 ) { self . inner . sin_port = hton ( new_port) }
118
133
}
119
134
@@ -143,12 +158,16 @@ impl SocketAddrV6 {
143
158
}
144
159
}
145
160
161
+ /// Change the IP address associated with this socket address.
162
+ #[ unstable( feature = "sockaddr_setters" , reason = "recent addition" , issue = "31572" ) ]
163
+ pub fn set_ip ( & mut self , new_ip : Ipv6Addr ) { self . inner . sin6_addr = * new_ip. as_inner ( ) }
164
+
146
165
/// Returns the port number associated with this socket address.
147
166
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
148
167
pub fn port ( & self ) -> u16 { ntoh ( self . inner . sin6_port ) }
149
168
150
169
/// Change the port number associated with this socket address.
151
- #[ unstable( feature = "sockaddr_set_port " , reason = "recent addition" , issue = "0 " ) ] // FIXME add tracking issue
170
+ #[ unstable( feature = "sockaddr_setters " , reason = "recent addition" , issue = "31572 " ) ]
152
171
pub fn set_port ( & mut self , new_port : u16 ) { self . inner . sin6_port = hton ( new_port) }
153
172
154
173
/// Returns the flow information associated with this address,
@@ -522,6 +541,36 @@ mod tests {
522
541
assert ! ( tsa( "1200::AB00:1234::2552:7777:1313:34300" ) . is_err( ) ) ;
523
542
}
524
543
544
+ #[ test]
545
+ fn set_ip ( ) {
546
+ fn ip4 ( low : u8 ) -> Ipv4Addr { Ipv4Addr :: new ( 77 , 88 , 21 , low) }
547
+ fn ip6 ( low : u16 ) -> Ipv6Addr { Ipv6Addr :: new ( 0x2a02 , 0x6b8 , 0 , 1 , 0 , 0 , 0 , low) }
548
+
549
+ let mut v4 = SocketAddrV4 :: new ( ip4 ( 11 ) , 80 ) ;
550
+ assert_eq ! ( v4. ip( ) , & ip4( 11 ) ) ;
551
+ v4. set_ip ( ip4 ( 12 ) ) ;
552
+ assert_eq ! ( v4. ip( ) , & ip4( 12 ) ) ;
553
+
554
+ let mut addr = SocketAddr :: V4 ( v4) ;
555
+ assert_eq ! ( addr. ip( ) , IpAddr :: V4 ( ip4( 12 ) ) ) ;
556
+ addr. set_ip ( IpAddr :: V4 ( ip4 ( 13 ) ) ) ;
557
+ assert_eq ! ( addr. ip( ) , IpAddr :: V4 ( ip4( 13 ) ) ) ;
558
+ addr. set_ip ( IpAddr :: V6 ( ip6 ( 14 ) ) ) ;
559
+ assert_eq ! ( addr. ip( ) , IpAddr :: V6 ( ip6( 14 ) ) ) ;
560
+
561
+ let mut v6 = SocketAddrV6 :: new ( ip6 ( 1 ) , 80 , 0 , 0 ) ;
562
+ assert_eq ! ( v6. ip( ) , & ip6( 1 ) ) ;
563
+ v6. set_ip ( ip6 ( 2 ) ) ;
564
+ assert_eq ! ( v6. ip( ) , & ip6( 2 ) ) ;
565
+
566
+ let mut addr = SocketAddr :: V6 ( v6) ;
567
+ assert_eq ! ( addr. ip( ) , IpAddr :: V6 ( ip6( 2 ) ) ) ;
568
+ addr. set_ip ( IpAddr :: V6 ( ip6 ( 3 ) ) ) ;
569
+ assert_eq ! ( addr. ip( ) , IpAddr :: V6 ( ip6( 3 ) ) ) ;
570
+ addr. set_ip ( IpAddr :: V4 ( ip4 ( 4 ) ) ) ;
571
+ assert_eq ! ( addr. ip( ) , IpAddr :: V4 ( ip4( 4 ) ) ) ;
572
+ }
573
+
525
574
#[ test]
526
575
fn set_port ( ) {
527
576
let mut v4 = SocketAddrV4 :: new ( Ipv4Addr :: new ( 77 , 88 , 21 , 11 ) , 80 ) ;
0 commit comments