Fix the non-readble IpAddr serialize implementations

This commit is contained in:
Markus Westerlind 2017-09-11 17:39:57 +02:00
parent c2474bf6ee
commit 85c05d301a
2 changed files with 27 additions and 5 deletions

View File

@ -513,8 +513,10 @@ impl Serialize for net::IpAddr {
}
} else {
match *self {
net::IpAddr::V4(ref a) => (0u8, a).serialize(serializer),
net::IpAddr::V6(ref a) => (1u8, a).serialize(serializer),
net::IpAddr::V4(ref a) =>
serializer.serialize_newtype_variant("IpAddr", 0, "V4", a),
net::IpAddr::V6(ref a) =>
serializer.serialize_newtype_variant("IpAddr", 1, "V6", a),
}
}
}
@ -558,9 +560,18 @@ impl Serialize for net::SocketAddr {
where
S: Serializer,
{
match *self {
net::SocketAddr::V4(ref addr) => addr.serialize(serializer),
net::SocketAddr::V6(ref addr) => addr.serialize(serializer),
if serializer.is_human_readable() {
match *self {
net::SocketAddr::V4(ref addr) => addr.serialize(serializer),
net::SocketAddr::V6(ref addr) => addr.serialize(serializer),
}
} else {
match *self {
net::SocketAddr::V4(ref addr) =>
serializer.serialize_newtype_variant("SocketAddr", 0, "V4", addr),
net::SocketAddr::V6(ref addr) =>
serializer.serialize_newtype_variant("SocketAddr", 1, "V6", addr),
}
}
}
}

View File

@ -426,8 +426,19 @@ declare_non_human_readable_tests!{
Token::TupleEnd,
],
}
test_non_human_readable_net_ipaddr {
net::IpAddr::from(*b"1234") => &seq![
Token::NewtypeVariant { name: "IpAddr", variant: "V4" },
Token::Tuple { len: 4 },
seq b"1234".iter().map(|&b| Token::U8(b)),
Token::TupleEnd,
],
}
test_non_human_readable_net_socketaddr {
net::SocketAddr::from((*b"1234567890123456", 1234)) => &seq![
Token::NewtypeVariant { name: "SocketAddr", variant: "V6" },
Token::Tuple { len: 2 },
Token::Tuple { len: 16 },