From 0a0db79044657e74c6e1588126bcbee1e249c196 Mon Sep 17 00:00:00 2001 From: Tom Schuster Date: Sat, 23 Aug 2025 17:10:48 +0200 Subject: [PATCH] Implement From for Host --- url/src/host.rs | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/url/src/host.rs b/url/src/host.rs index bfe1e2c9e..e4f69cdd3 100644 --- a/url/src/host.rs +++ b/url/src/host.rs @@ -6,7 +6,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use crate::net::{Ipv4Addr, Ipv6Addr}; +use crate::net::{IpAddr, Ipv4Addr, Ipv6Addr}; use alloc::borrow::Cow; use alloc::borrow::ToOwned; use alloc::string::String; @@ -200,6 +200,27 @@ where } } +impl From for Host { + fn from(ipv4: Ipv4Addr) -> Self { + Self::Ipv4(ipv4) + } +} + +impl From for Host { + fn from(ipv6: Ipv6Addr) -> Self { + Self::Ipv6(ipv6) + } +} + +impl From for Host { + fn from(ip: IpAddr) -> Self { + match ip { + IpAddr::V4(ipv4) => ipv4.into(), + IpAddr::V6(ipv6) => ipv6.into(), + } + } +} + fn write_ipv6(addr: &Ipv6Addr, f: &mut Formatter<'_>) -> fmt::Result { let segments = addr.segments(); let (compress_start, compress_end) = longest_zero_sequence(&segments);