Skip to content

Commit d324917

Browse files
committed
auto merge of #12149 : thomaslee/rust/ipaddr_deriving_iter_bytes, r=cmr
This is a fairly trivial (but IMHO handy) change to implement IterBytes for IpAddr and SocketAddr. I originally stumbled across this because I wanted to use a SocketAddr as a HashMap key and discovered that I couldn't do it directly. Had to impl IterBytes on a new intermediate type to work around it.
2 parents d440a56 + e205185 commit d324917

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

src/libstd/io/net/ip.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,12 @@ use iter::Iterator;
1414
use option::{Option, None, Some};
1515
use str::StrSlice;
1616
use to_str::ToStr;
17+
use to_bytes::IterBytes;
1718
use vec::{MutableCloneableVector, ImmutableVector, MutableVector};
1819

1920
pub type Port = u16;
2021

21-
#[deriving(Eq, TotalEq, Clone)]
22+
#[deriving(Eq, TotalEq, Clone, IterBytes)]
2223
pub enum IpAddr {
2324
Ipv4Addr(u8, u8, u8, u8),
2425
Ipv6Addr(u16, u16, u16, u16, u16, u16, u16, u16)
@@ -48,7 +49,7 @@ impl ToStr for IpAddr {
4849
}
4950
}
5051

51-
#[deriving(Eq, TotalEq, Clone)]
52+
#[deriving(Eq, TotalEq, Clone, IterBytes)]
5253
pub struct SocketAddr {
5354
ip: IpAddr,
5455
port: Port,
@@ -339,6 +340,7 @@ impl FromStr for SocketAddr {
339340
mod test {
340341
use prelude::*;
341342
use super::*;
343+
use to_bytes::ToBytes;
342344

343345
#[test]
344346
fn test_from_str_ipv4() {
@@ -441,4 +443,13 @@ mod test {
441443
assert_eq!(Ipv6Addr(8, 9, 10, 11, 12, 13, 14, 15).to_str(), ~"8:9:a:b:c:d:e:f");
442444
}
443445

446+
#[test]
447+
fn ipv4_addr_to_bytes() {
448+
Ipv4Addr(123, 20, 12, 56).to_bytes(true);
449+
}
450+
451+
#[test]
452+
fn socket_addr_to_bytes() {
453+
SocketAddr { ip: Ipv4Addr(1, 2, 3, 4), port: 1234 }.to_bytes(true);
454+
}
444455
}

0 commit comments

Comments
 (0)