Skip to content

Commit 4e909f6

Browse files
author
bors-servo
authored
Auto merge of #328 - tomecki:display_for_host_and_port, r=SimonSapin
Add Display for HostAndPort Fix #300 <!-- Reviewable:start --> --- This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/rust-url/328) <!-- Reviewable:end -->
2 parents 923c02f + a6421f6 commit 4e909f6

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

src/host.rs

+9
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,15 @@ impl<'a> HostAndPort<&'a str> {
192192
}
193193
}
194194

195+
impl<S: AsRef<str>> fmt::Display for HostAndPort<S> {
196+
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
197+
self.host.fmt(f)?;
198+
f.write_str(":")?;
199+
self.port.fmt(f)
200+
}
201+
}
202+
203+
195204
impl<S: AsRef<str>> ToSocketAddrs for HostAndPort<S> {
196205
type Iter = SocketAddrs;
197206

tests/unit.rs

+31-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ extern crate url;
1414
use std::borrow::Cow;
1515
use std::net::{Ipv4Addr, Ipv6Addr};
1616
use std::path::{Path, PathBuf};
17-
use url::{Host, Url, form_urlencoded};
17+
use url::{Host, HostAndPort, Url, form_urlencoded};
1818

1919
#[test]
2020
fn size() {
@@ -255,6 +255,36 @@ fn test_form_serialize() {
255255
assert_eq!(encoded, "foo=%C3%A9%26&bar=&foo=%23");
256256
}
257257

258+
#[test]
259+
fn host_and_port_display() {
260+
assert_eq!(
261+
format!(
262+
"{}",
263+
HostAndPort{ host: Host::Domain("www.mozilla.org"), port: 80}
264+
),
265+
"www.mozilla.org:80"
266+
);
267+
assert_eq!(
268+
format!(
269+
"{}",
270+
HostAndPort::<String>{ host: Host::Ipv4(Ipv4Addr::new(1, 35, 33, 49)), port: 65535 }
271+
),
272+
"1.35.33.49:65535"
273+
);
274+
assert_eq!(
275+
format!(
276+
"{}",
277+
HostAndPort::<String>{
278+
host: Host::Ipv6(Ipv6Addr::new(
279+
0x2001, 0x0db8, 0x85a3, 0x08d3, 0x1319, 0x8a2e, 0x0370, 0x7344
280+
)),
281+
port: 1337
282+
})
283+
,
284+
"[2001:db8:85a3:8d3:1319:8a2e:370:7344]:1337"
285+
)
286+
}
287+
258288
#[test]
259289
/// https://github.com/servo/rust-url/issues/25
260290
fn issue_25() {

0 commit comments

Comments
 (0)