Skip to content

IterBytes for IpAddr and SocketAddr #12149

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 10, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/libstd/io/net/ip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ use iter::Iterator;
use option::{Option, None, Some};
use str::StrSlice;
use to_str::ToStr;
use to_bytes::IterBytes;
use vec::{MutableCloneableVector, ImmutableVector, MutableVector};

pub type Port = u16;

#[deriving(Eq, TotalEq, Clone)]
#[deriving(Eq, TotalEq, Clone, IterBytes)]
pub enum IpAddr {
Ipv4Addr(u8, u8, u8, u8),
Ipv6Addr(u16, u16, u16, u16, u16, u16, u16, u16)
Expand Down Expand Up @@ -48,7 +49,7 @@ impl ToStr for IpAddr {
}
}

#[deriving(Eq, TotalEq, Clone)]
#[deriving(Eq, TotalEq, Clone, IterBytes)]
pub struct SocketAddr {
ip: IpAddr,
port: Port,
Expand Down Expand Up @@ -339,6 +340,7 @@ impl FromStr for SocketAddr {
mod test {
use prelude::*;
use super::*;
use to_bytes::ToBytes;

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

#[test]
fn ipv4_addr_to_bytes() {
Ipv4Addr(123, 20, 12, 56).to_bytes(true);
}

#[test]
fn socket_addr_to_bytes() {
SocketAddr { ip: Ipv4Addr(1, 2, 3, 4), port: 1234 }.to_bytes(true);
}
}