Skip to content

Commit 0a0db79

Browse files
committed
Implement From<IpAddr> for Host
1 parent 2387efa commit 0a0db79

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

url/src/host.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
// option. This file may not be copied, modified, or distributed
77
// except according to those terms.
88

9-
use crate::net::{Ipv4Addr, Ipv6Addr};
9+
use crate::net::{IpAddr, Ipv4Addr, Ipv6Addr};
1010
use alloc::borrow::Cow;
1111
use alloc::borrow::ToOwned;
1212
use alloc::string::String;
@@ -200,6 +200,27 @@ where
200200
}
201201
}
202202

203+
impl From<Ipv4Addr> for Host {
204+
fn from(ipv4: Ipv4Addr) -> Self {
205+
Self::Ipv4(ipv4)
206+
}
207+
}
208+
209+
impl From<Ipv6Addr> for Host {
210+
fn from(ipv6: Ipv6Addr) -> Self {
211+
Self::Ipv6(ipv6)
212+
}
213+
}
214+
215+
impl From<IpAddr> for Host {
216+
fn from(ip: IpAddr) -> Self {
217+
match ip {
218+
IpAddr::V4(ipv4) => ipv4.into(),
219+
IpAddr::V6(ipv6) => ipv6.into(),
220+
}
221+
}
222+
}
223+
203224
fn write_ipv6(addr: &Ipv6Addr, f: &mut Formatter<'_>) -> fmt::Result {
204225
let segments = addr.segments();
205226
let (compress_start, compress_end) = longest_zero_sequence(&segments);

0 commit comments

Comments
 (0)