@@ -67,6 +67,17 @@ impl SocketAddr {
6767 }
6868 }
6969
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+
7081 /// Returns the port number associated with this socket address.
7182 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
7283 pub fn port ( & self ) -> u16 {
@@ -77,7 +88,7 @@ impl SocketAddr {
7788 }
7889
7990 /// 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 " ) ]
8192 pub fn set_port ( & mut self , new_port : u16 ) {
8293 match * self {
8394 SocketAddr :: V4 ( ref mut a) => a. set_port ( new_port) ,
@@ -108,12 +119,16 @@ impl SocketAddrV4 {
108119 }
109120 }
110121
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+
111126 /// Returns the port number associated with this socket address.
112127 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
113128 pub fn port ( & self ) -> u16 { ntoh ( self . inner . sin_port ) }
114129
115130 /// 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 " ) ]
117132 pub fn set_port ( & mut self , new_port : u16 ) { self . inner . sin_port = hton ( new_port) }
118133}
119134
@@ -143,12 +158,16 @@ impl SocketAddrV6 {
143158 }
144159 }
145160
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+
146165 /// Returns the port number associated with this socket address.
147166 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
148167 pub fn port ( & self ) -> u16 { ntoh ( self . inner . sin6_port ) }
149168
150169 /// 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 " ) ]
152171 pub fn set_port ( & mut self , new_port : u16 ) { self . inner . sin6_port = hton ( new_port) }
153172
154173 /// Returns the flow information associated with this address,
@@ -522,6 +541,36 @@ mod tests {
522541 assert ! ( tsa( "1200::AB00:1234::2552:7777:1313:34300" ) . is_err( ) ) ;
523542 }
524543
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+
525574 #[ test]
526575 fn set_port ( ) {
527576 let mut v4 = SocketAddrV4 :: new ( Ipv4Addr :: new ( 77 , 88 , 21 , 11 ) , 80 ) ;
0 commit comments