Skip to content

Commit cd01366

Browse files
committed
Add SocketAddr{,V4,V6}::set_port.
As demonstrated in the `resolve_socket_addr` change, this is less awkward than re-creating a new address from the other parts.
1 parent f5f8e0b commit cd01366

File tree

1 file changed

+43
-10
lines changed

1 file changed

+43
-10
lines changed

src/libstd/net/addr.rs

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,15 @@ impl SocketAddr {
7575
SocketAddr::V6(ref a) => a.port(),
7676
}
7777
}
78+
79+
/// Change the port number associated with this socket address.
80+
#[unstable(feature = "sockaddr_set_port", reason = "recent addition", issue = "0")] // FIXME add tracking issue
81+
pub fn set_port(&mut self, new_port: u16) {
82+
match *self {
83+
SocketAddr::V4(ref mut a) => a.set_port(new_port),
84+
SocketAddr::V6(ref mut a) => a.set_port(new_port),
85+
}
86+
}
7887
}
7988

8089
impl SocketAddrV4 {
@@ -102,6 +111,10 @@ impl SocketAddrV4 {
102111
/// Returns the port number associated with this socket address.
103112
#[stable(feature = "rust1", since = "1.0.0")]
104113
pub fn port(&self) -> u16 { ntoh(self.inner.sin_port) }
114+
115+
/// Change the port number associated with this socket address.
116+
#[unstable(feature = "sockaddr_set_port", reason = "recent addition", issue = "0")] // FIXME add tracking issue
117+
pub fn set_port(&mut self, new_port: u16) { self.inner.sin_port = hton(new_port) }
105118
}
106119

107120
impl SocketAddrV6 {
@@ -134,6 +147,10 @@ impl SocketAddrV6 {
134147
#[stable(feature = "rust1", since = "1.0.0")]
135148
pub fn port(&self) -> u16 { ntoh(self.inner.sin6_port) }
136149

150+
/// Change the port number associated with this socket address.
151+
#[unstable(feature = "sockaddr_set_port", reason = "recent addition", issue = "0")] // FIXME add tracking issue
152+
pub fn set_port(&mut self, new_port: u16) { self.inner.sin6_port = hton(new_port) }
153+
137154
/// Returns the flow information associated with this address,
138155
/// corresponding to the `sin6_flowinfo` field in C.
139156
#[stable(feature = "rust1", since = "1.0.0")]
@@ -385,16 +402,9 @@ impl ToSocketAddrs for (Ipv6Addr, u16) {
385402
fn resolve_socket_addr(s: &str, p: u16) -> io::Result<vec::IntoIter<SocketAddr>> {
386403
let ips = try!(lookup_host(s));
387404
let v: Vec<_> = try!(ips.map(|a| {
388-
a.map(|a| {
389-
match a {
390-
SocketAddr::V4(ref a) => {
391-
SocketAddr::V4(SocketAddrV4::new(*a.ip(), p))
392-
}
393-
SocketAddr::V6(ref a) => {
394-
SocketAddr::V6(SocketAddrV6::new(*a.ip(), p, a.flowinfo(),
395-
a.scope_id()))
396-
}
397-
}
405+
a.map(|mut a| {
406+
a.set_port(p);
407+
a
398408
})
399409
}).collect());
400410
Ok(v.into_iter())
@@ -511,4 +521,27 @@ mod tests {
511521
fn to_socket_addr_str_bad() {
512522
assert!(tsa("1200::AB00:1234::2552:7777:1313:34300").is_err());
513523
}
524+
525+
#[test]
526+
fn set_port() {
527+
let mut v4 = SocketAddrV4::new(Ipv4Addr::new(77, 88, 21, 11), 80);
528+
assert_eq!(v4.port(), 80);
529+
v4.set_port(443);
530+
assert_eq!(v4.port(), 443);
531+
532+
let mut addr = SocketAddr::V4(v4);
533+
assert_eq!(addr.port(), 443);
534+
addr.set_port(8080);
535+
assert_eq!(addr.port(), 8080);
536+
537+
let mut v6 = SocketAddrV6::new(Ipv6Addr::new(0x2a02, 0x6b8, 0, 1, 0, 0, 0, 1), 80, 0, 0);
538+
assert_eq!(v6.port(), 80);
539+
v6.set_port(443);
540+
assert_eq!(v6.port(), 443);
541+
542+
let mut addr = SocketAddr::V6(v6);
543+
assert_eq!(addr.port(), 443);
544+
addr.set_port(8080);
545+
assert_eq!(addr.port(), 8080);
546+
}
514547
}

0 commit comments

Comments
 (0)