1b8a62b91Sopenharmony_ci//! The following is derived from Rust's 2b8a62b91Sopenharmony_ci//! library/std/src/net/socket_addr.rs at revision 3b8a62b91Sopenharmony_ci//! f7e8ba28a4785e698a55fb95e4b3e803302de0ff. 4b8a62b91Sopenharmony_ci//! 5b8a62b91Sopenharmony_ci//! All code in this file is licensed MIT or Apache 2.0 at your option. 6b8a62b91Sopenharmony_ci//! 7b8a62b91Sopenharmony_ci//! This defines `SocketAddr`, `SocketAddrV4`, and `SocketAddrV6` in a 8b8a62b91Sopenharmony_ci//! platform-independent way. It is not the native representation. 9b8a62b91Sopenharmony_ci 10b8a62b91Sopenharmony_ci#![allow(unsafe_code)] 11b8a62b91Sopenharmony_ci 12b8a62b91Sopenharmony_ciuse crate::net::ip::{IpAddr, Ipv4Addr, Ipv6Addr}; 13b8a62b91Sopenharmony_ciuse core::cmp::Ordering; 14b8a62b91Sopenharmony_ciuse core::hash; 15b8a62b91Sopenharmony_ci 16b8a62b91Sopenharmony_ci/// An internet socket address, either IPv4 or IPv6. 17b8a62b91Sopenharmony_ci/// 18b8a62b91Sopenharmony_ci/// Internet socket addresses consist of an [IP address], a 16-bit port number, as well 19b8a62b91Sopenharmony_ci/// as possibly some version-dependent additional information. See [`SocketAddrV4`]'s and 20b8a62b91Sopenharmony_ci/// [`SocketAddrV6`]'s respective documentation for more details. 21b8a62b91Sopenharmony_ci/// 22b8a62b91Sopenharmony_ci/// The size of a `SocketAddr` instance may vary depending on the target operating 23b8a62b91Sopenharmony_ci/// system. 24b8a62b91Sopenharmony_ci/// 25b8a62b91Sopenharmony_ci/// [IP address]: IpAddr 26b8a62b91Sopenharmony_ci/// 27b8a62b91Sopenharmony_ci/// # Examples 28b8a62b91Sopenharmony_ci/// 29b8a62b91Sopenharmony_ci/// ``` 30b8a62b91Sopenharmony_ci/// use std::net::{IpAddr, Ipv4Addr, SocketAddr}; 31b8a62b91Sopenharmony_ci/// 32b8a62b91Sopenharmony_ci/// let socket = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8080); 33b8a62b91Sopenharmony_ci/// 34b8a62b91Sopenharmony_ci/// assert_eq!("127.0.0.1:8080".parse(), Ok(socket)); 35b8a62b91Sopenharmony_ci/// assert_eq!(socket.port(), 8080); 36b8a62b91Sopenharmony_ci/// assert_eq!(socket.is_ipv4(), true); 37b8a62b91Sopenharmony_ci/// ``` 38b8a62b91Sopenharmony_ci#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)] 39b8a62b91Sopenharmony_ci#[cfg_attr(staged_api, stable(feature = "rust1", since = "1.0.0"))] 40b8a62b91Sopenharmony_cipub enum SocketAddr { 41b8a62b91Sopenharmony_ci /// An IPv4 socket address. 42b8a62b91Sopenharmony_ci #[cfg_attr(staged_api, stable(feature = "rust1", since = "1.0.0"))] 43b8a62b91Sopenharmony_ci V4(#[cfg_attr(staged_api, stable(feature = "rust1", since = "1.0.0"))] SocketAddrV4), 44b8a62b91Sopenharmony_ci /// An IPv6 socket address. 45b8a62b91Sopenharmony_ci #[cfg_attr(staged_api, stable(feature = "rust1", since = "1.0.0"))] 46b8a62b91Sopenharmony_ci V6(#[cfg_attr(staged_api, stable(feature = "rust1", since = "1.0.0"))] SocketAddrV6), 47b8a62b91Sopenharmony_ci} 48b8a62b91Sopenharmony_ci 49b8a62b91Sopenharmony_ci/// An IPv4 socket address. 50b8a62b91Sopenharmony_ci/// 51b8a62b91Sopenharmony_ci/// IPv4 socket addresses consist of an [`IPv4` address] and a 16-bit port number, as 52b8a62b91Sopenharmony_ci/// stated in [IETF RFC 793]. 53b8a62b91Sopenharmony_ci/// 54b8a62b91Sopenharmony_ci/// See [`SocketAddr`] for a type encompassing both IPv4 and IPv6 socket addresses. 55b8a62b91Sopenharmony_ci/// 56b8a62b91Sopenharmony_ci/// The size of a `SocketAddrV4` struct may vary depending on the target operating 57b8a62b91Sopenharmony_ci/// system. Do not assume that this type has the same memory layout as the underlying 58b8a62b91Sopenharmony_ci/// system representation. 59b8a62b91Sopenharmony_ci/// 60b8a62b91Sopenharmony_ci/// [IETF RFC 793]: https://tools.ietf.org/html/rfc793 61b8a62b91Sopenharmony_ci/// [`IPv4` address]: Ipv4Addr 62b8a62b91Sopenharmony_ci/// 63b8a62b91Sopenharmony_ci/// # Examples 64b8a62b91Sopenharmony_ci/// 65b8a62b91Sopenharmony_ci/// ``` 66b8a62b91Sopenharmony_ci/// use std::net::{Ipv4Addr, SocketAddrV4}; 67b8a62b91Sopenharmony_ci/// 68b8a62b91Sopenharmony_ci/// let socket = SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 8080); 69b8a62b91Sopenharmony_ci/// 70b8a62b91Sopenharmony_ci/// assert_eq!("127.0.0.1:8080".parse(), Ok(socket)); 71b8a62b91Sopenharmony_ci/// assert_eq!(socket.ip(), &Ipv4Addr::new(127, 0, 0, 1)); 72b8a62b91Sopenharmony_ci/// assert_eq!(socket.port(), 8080); 73b8a62b91Sopenharmony_ci/// ``` 74b8a62b91Sopenharmony_ci#[derive(Copy, Clone, Eq, PartialEq)] 75b8a62b91Sopenharmony_ci#[cfg_attr(staged_api, stable(feature = "rust1", since = "1.0.0"))] 76b8a62b91Sopenharmony_cipub struct SocketAddrV4 { 77b8a62b91Sopenharmony_ci ip: Ipv4Addr, 78b8a62b91Sopenharmony_ci port: u16, 79b8a62b91Sopenharmony_ci} 80b8a62b91Sopenharmony_ci 81b8a62b91Sopenharmony_ci/// An IPv6 socket address. 82b8a62b91Sopenharmony_ci/// 83b8a62b91Sopenharmony_ci/// IPv6 socket addresses consist of an [`IPv6` address], a 16-bit port number, as well 84b8a62b91Sopenharmony_ci/// as fields containing the traffic class, the flow label, and a scope identifier 85b8a62b91Sopenharmony_ci/// (see [IETF RFC 2553, Section 3.3] for more details). 86b8a62b91Sopenharmony_ci/// 87b8a62b91Sopenharmony_ci/// See [`SocketAddr`] for a type encompassing both IPv4 and IPv6 socket addresses. 88b8a62b91Sopenharmony_ci/// 89b8a62b91Sopenharmony_ci/// The size of a `SocketAddrV6` struct may vary depending on the target operating 90b8a62b91Sopenharmony_ci/// system. Do not assume that this type has the same memory layout as the underlying 91b8a62b91Sopenharmony_ci/// system representation. 92b8a62b91Sopenharmony_ci/// 93b8a62b91Sopenharmony_ci/// [IETF RFC 2553, Section 3.3]: https://tools.ietf.org/html/rfc2553#section-3.3 94b8a62b91Sopenharmony_ci/// [`IPv6` address]: Ipv6Addr 95b8a62b91Sopenharmony_ci/// 96b8a62b91Sopenharmony_ci/// # Examples 97b8a62b91Sopenharmony_ci/// 98b8a62b91Sopenharmony_ci/// ``` 99b8a62b91Sopenharmony_ci/// use std::net::{Ipv6Addr, SocketAddrV6}; 100b8a62b91Sopenharmony_ci/// 101b8a62b91Sopenharmony_ci/// let socket = SocketAddrV6::new(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 1), 8080, 0, 0); 102b8a62b91Sopenharmony_ci/// 103b8a62b91Sopenharmony_ci/// assert_eq!("[2001:db8::1]:8080".parse(), Ok(socket)); 104b8a62b91Sopenharmony_ci/// assert_eq!(socket.ip(), &Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 1)); 105b8a62b91Sopenharmony_ci/// assert_eq!(socket.port(), 8080); 106b8a62b91Sopenharmony_ci/// ``` 107b8a62b91Sopenharmony_ci#[derive(Copy, Clone, Eq, PartialEq)] 108b8a62b91Sopenharmony_ci#[cfg_attr(staged_api, stable(feature = "rust1", since = "1.0.0"))] 109b8a62b91Sopenharmony_cipub struct SocketAddrV6 { 110b8a62b91Sopenharmony_ci ip: Ipv6Addr, 111b8a62b91Sopenharmony_ci port: u16, 112b8a62b91Sopenharmony_ci flowinfo: u32, 113b8a62b91Sopenharmony_ci scope_id: u32, 114b8a62b91Sopenharmony_ci} 115b8a62b91Sopenharmony_ci 116b8a62b91Sopenharmony_ciimpl SocketAddr { 117b8a62b91Sopenharmony_ci /// Creates a new socket address from an [IP address] and a port number. 118b8a62b91Sopenharmony_ci /// 119b8a62b91Sopenharmony_ci /// [IP address]: IpAddr 120b8a62b91Sopenharmony_ci /// 121b8a62b91Sopenharmony_ci /// # Examples 122b8a62b91Sopenharmony_ci /// 123b8a62b91Sopenharmony_ci /// ``` 124b8a62b91Sopenharmony_ci /// use std::net::{IpAddr, Ipv4Addr, SocketAddr}; 125b8a62b91Sopenharmony_ci /// 126b8a62b91Sopenharmony_ci /// let socket = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8080); 127b8a62b91Sopenharmony_ci /// assert_eq!(socket.ip(), IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1))); 128b8a62b91Sopenharmony_ci /// assert_eq!(socket.port(), 8080); 129b8a62b91Sopenharmony_ci /// ``` 130b8a62b91Sopenharmony_ci #[cfg_attr(staged_api, stable(feature = "ip_addr", since = "1.7.0"))] 131b8a62b91Sopenharmony_ci #[must_use] 132b8a62b91Sopenharmony_ci #[cfg_attr( 133b8a62b91Sopenharmony_ci staged_api, 134b8a62b91Sopenharmony_ci rustc_const_unstable(feature = "const_socketaddr", issue = "82485") 135b8a62b91Sopenharmony_ci )] 136b8a62b91Sopenharmony_ci pub const fn new(ip: IpAddr, port: u16) -> SocketAddr { 137b8a62b91Sopenharmony_ci match ip { 138b8a62b91Sopenharmony_ci IpAddr::V4(a) => SocketAddr::V4(SocketAddrV4::new(a, port)), 139b8a62b91Sopenharmony_ci IpAddr::V6(a) => SocketAddr::V6(SocketAddrV6::new(a, port, 0, 0)), 140b8a62b91Sopenharmony_ci } 141b8a62b91Sopenharmony_ci } 142b8a62b91Sopenharmony_ci 143b8a62b91Sopenharmony_ci /// Returns the IP address associated with this socket address. 144b8a62b91Sopenharmony_ci /// 145b8a62b91Sopenharmony_ci /// # Examples 146b8a62b91Sopenharmony_ci /// 147b8a62b91Sopenharmony_ci /// ``` 148b8a62b91Sopenharmony_ci /// use std::net::{IpAddr, Ipv4Addr, SocketAddr}; 149b8a62b91Sopenharmony_ci /// 150b8a62b91Sopenharmony_ci /// let socket = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8080); 151b8a62b91Sopenharmony_ci /// assert_eq!(socket.ip(), IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1))); 152b8a62b91Sopenharmony_ci /// ``` 153b8a62b91Sopenharmony_ci #[must_use] 154b8a62b91Sopenharmony_ci #[cfg_attr(staged_api, stable(feature = "ip_addr", since = "1.7.0"))] 155b8a62b91Sopenharmony_ci #[cfg_attr( 156b8a62b91Sopenharmony_ci staged_api, 157b8a62b91Sopenharmony_ci rustc_const_unstable(feature = "const_socketaddr", issue = "82485") 158b8a62b91Sopenharmony_ci )] 159b8a62b91Sopenharmony_ci pub const fn ip(&self) -> IpAddr { 160b8a62b91Sopenharmony_ci match *self { 161b8a62b91Sopenharmony_ci SocketAddr::V4(ref a) => IpAddr::V4(*a.ip()), 162b8a62b91Sopenharmony_ci SocketAddr::V6(ref a) => IpAddr::V6(*a.ip()), 163b8a62b91Sopenharmony_ci } 164b8a62b91Sopenharmony_ci } 165b8a62b91Sopenharmony_ci 166b8a62b91Sopenharmony_ci /// Changes the IP address associated with this socket address. 167b8a62b91Sopenharmony_ci /// 168b8a62b91Sopenharmony_ci /// # Examples 169b8a62b91Sopenharmony_ci /// 170b8a62b91Sopenharmony_ci /// ``` 171b8a62b91Sopenharmony_ci /// use std::net::{IpAddr, Ipv4Addr, SocketAddr}; 172b8a62b91Sopenharmony_ci /// 173b8a62b91Sopenharmony_ci /// let mut socket = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8080); 174b8a62b91Sopenharmony_ci /// socket.set_ip(IpAddr::V4(Ipv4Addr::new(10, 10, 0, 1))); 175b8a62b91Sopenharmony_ci /// assert_eq!(socket.ip(), IpAddr::V4(Ipv4Addr::new(10, 10, 0, 1))); 176b8a62b91Sopenharmony_ci /// ``` 177b8a62b91Sopenharmony_ci #[cfg_attr(staged_api, stable(feature = "sockaddr_setters", since = "1.9.0"))] 178b8a62b91Sopenharmony_ci pub fn set_ip(&mut self, new_ip: IpAddr) { 179b8a62b91Sopenharmony_ci // `match (*self, new_ip)` would have us mutate a copy of self only to throw it away. 180b8a62b91Sopenharmony_ci match (self, new_ip) { 181b8a62b91Sopenharmony_ci (&mut SocketAddr::V4(ref mut a), IpAddr::V4(new_ip)) => a.set_ip(new_ip), 182b8a62b91Sopenharmony_ci (&mut SocketAddr::V6(ref mut a), IpAddr::V6(new_ip)) => a.set_ip(new_ip), 183b8a62b91Sopenharmony_ci (self_, new_ip) => *self_ = Self::new(new_ip, self_.port()), 184b8a62b91Sopenharmony_ci } 185b8a62b91Sopenharmony_ci } 186b8a62b91Sopenharmony_ci 187b8a62b91Sopenharmony_ci /// Returns the port number associated with this socket address. 188b8a62b91Sopenharmony_ci /// 189b8a62b91Sopenharmony_ci /// # Examples 190b8a62b91Sopenharmony_ci /// 191b8a62b91Sopenharmony_ci /// ``` 192b8a62b91Sopenharmony_ci /// use std::net::{IpAddr, Ipv4Addr, SocketAddr}; 193b8a62b91Sopenharmony_ci /// 194b8a62b91Sopenharmony_ci /// let socket = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8080); 195b8a62b91Sopenharmony_ci /// assert_eq!(socket.port(), 8080); 196b8a62b91Sopenharmony_ci /// ``` 197b8a62b91Sopenharmony_ci #[must_use] 198b8a62b91Sopenharmony_ci #[cfg_attr(staged_api, stable(feature = "rust1", since = "1.0.0"))] 199b8a62b91Sopenharmony_ci #[cfg_attr( 200b8a62b91Sopenharmony_ci staged_api, 201b8a62b91Sopenharmony_ci rustc_const_unstable(feature = "const_socketaddr", issue = "82485") 202b8a62b91Sopenharmony_ci )] 203b8a62b91Sopenharmony_ci pub const fn port(&self) -> u16 { 204b8a62b91Sopenharmony_ci match *self { 205b8a62b91Sopenharmony_ci SocketAddr::V4(ref a) => a.port(), 206b8a62b91Sopenharmony_ci SocketAddr::V6(ref a) => a.port(), 207b8a62b91Sopenharmony_ci } 208b8a62b91Sopenharmony_ci } 209b8a62b91Sopenharmony_ci 210b8a62b91Sopenharmony_ci /// Changes the port number associated with this socket address. 211b8a62b91Sopenharmony_ci /// 212b8a62b91Sopenharmony_ci /// # Examples 213b8a62b91Sopenharmony_ci /// 214b8a62b91Sopenharmony_ci /// ``` 215b8a62b91Sopenharmony_ci /// use std::net::{IpAddr, Ipv4Addr, SocketAddr}; 216b8a62b91Sopenharmony_ci /// 217b8a62b91Sopenharmony_ci /// let mut socket = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8080); 218b8a62b91Sopenharmony_ci /// socket.set_port(1025); 219b8a62b91Sopenharmony_ci /// assert_eq!(socket.port(), 1025); 220b8a62b91Sopenharmony_ci /// ``` 221b8a62b91Sopenharmony_ci #[cfg_attr(staged_api, stable(feature = "sockaddr_setters", since = "1.9.0"))] 222b8a62b91Sopenharmony_ci pub fn set_port(&mut self, new_port: u16) { 223b8a62b91Sopenharmony_ci match *self { 224b8a62b91Sopenharmony_ci SocketAddr::V4(ref mut a) => a.set_port(new_port), 225b8a62b91Sopenharmony_ci SocketAddr::V6(ref mut a) => a.set_port(new_port), 226b8a62b91Sopenharmony_ci } 227b8a62b91Sopenharmony_ci } 228b8a62b91Sopenharmony_ci 229b8a62b91Sopenharmony_ci /// Returns [`true`] if the [IP address] in this `SocketAddr` is an 230b8a62b91Sopenharmony_ci /// [`IPv4` address], and [`false`] otherwise. 231b8a62b91Sopenharmony_ci /// 232b8a62b91Sopenharmony_ci /// [IP address]: IpAddr 233b8a62b91Sopenharmony_ci /// [`IPv4` address]: IpAddr::V4 234b8a62b91Sopenharmony_ci /// 235b8a62b91Sopenharmony_ci /// # Examples 236b8a62b91Sopenharmony_ci /// 237b8a62b91Sopenharmony_ci /// ``` 238b8a62b91Sopenharmony_ci /// use std::net::{IpAddr, Ipv4Addr, SocketAddr}; 239b8a62b91Sopenharmony_ci /// 240b8a62b91Sopenharmony_ci /// let socket = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8080); 241b8a62b91Sopenharmony_ci /// assert_eq!(socket.is_ipv4(), true); 242b8a62b91Sopenharmony_ci /// assert_eq!(socket.is_ipv6(), false); 243b8a62b91Sopenharmony_ci /// ``` 244b8a62b91Sopenharmony_ci #[must_use] 245b8a62b91Sopenharmony_ci #[cfg_attr(staged_api, stable(feature = "sockaddr_checker", since = "1.16.0"))] 246b8a62b91Sopenharmony_ci #[cfg_attr( 247b8a62b91Sopenharmony_ci staged_api, 248b8a62b91Sopenharmony_ci rustc_const_unstable(feature = "const_socketaddr", issue = "82485") 249b8a62b91Sopenharmony_ci )] 250b8a62b91Sopenharmony_ci pub const fn is_ipv4(&self) -> bool { 251b8a62b91Sopenharmony_ci matches!(*self, SocketAddr::V4(_)) 252b8a62b91Sopenharmony_ci } 253b8a62b91Sopenharmony_ci 254b8a62b91Sopenharmony_ci /// Returns [`true`] if the [IP address] in this `SocketAddr` is an 255b8a62b91Sopenharmony_ci /// [`IPv6` address], and [`false`] otherwise. 256b8a62b91Sopenharmony_ci /// 257b8a62b91Sopenharmony_ci /// [IP address]: IpAddr 258b8a62b91Sopenharmony_ci /// [`IPv6` address]: IpAddr::V6 259b8a62b91Sopenharmony_ci /// 260b8a62b91Sopenharmony_ci /// # Examples 261b8a62b91Sopenharmony_ci /// 262b8a62b91Sopenharmony_ci /// ``` 263b8a62b91Sopenharmony_ci /// use std::net::{IpAddr, Ipv6Addr, SocketAddr}; 264b8a62b91Sopenharmony_ci /// 265b8a62b91Sopenharmony_ci /// let socket = SocketAddr::new(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 65535, 0, 1)), 8080); 266b8a62b91Sopenharmony_ci /// assert_eq!(socket.is_ipv4(), false); 267b8a62b91Sopenharmony_ci /// assert_eq!(socket.is_ipv6(), true); 268b8a62b91Sopenharmony_ci /// ``` 269b8a62b91Sopenharmony_ci #[must_use] 270b8a62b91Sopenharmony_ci #[cfg_attr(staged_api, stable(feature = "sockaddr_checker", since = "1.16.0"))] 271b8a62b91Sopenharmony_ci #[cfg_attr( 272b8a62b91Sopenharmony_ci staged_api, 273b8a62b91Sopenharmony_ci rustc_const_unstable(feature = "const_socketaddr", issue = "82485") 274b8a62b91Sopenharmony_ci )] 275b8a62b91Sopenharmony_ci pub const fn is_ipv6(&self) -> bool { 276b8a62b91Sopenharmony_ci matches!(*self, SocketAddr::V6(_)) 277b8a62b91Sopenharmony_ci } 278b8a62b91Sopenharmony_ci} 279b8a62b91Sopenharmony_ci 280b8a62b91Sopenharmony_ciimpl SocketAddrV4 { 281b8a62b91Sopenharmony_ci /// Creates a new socket address from an [`IPv4` address] and a port number. 282b8a62b91Sopenharmony_ci /// 283b8a62b91Sopenharmony_ci /// [`IPv4` address]: Ipv4Addr 284b8a62b91Sopenharmony_ci /// 285b8a62b91Sopenharmony_ci /// # Examples 286b8a62b91Sopenharmony_ci /// 287b8a62b91Sopenharmony_ci /// ``` 288b8a62b91Sopenharmony_ci /// use std::net::{SocketAddrV4, Ipv4Addr}; 289b8a62b91Sopenharmony_ci /// 290b8a62b91Sopenharmony_ci /// let socket = SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 8080); 291b8a62b91Sopenharmony_ci /// ``` 292b8a62b91Sopenharmony_ci #[cfg_attr(staged_api, stable(feature = "rust1", since = "1.0.0"))] 293b8a62b91Sopenharmony_ci #[must_use] 294b8a62b91Sopenharmony_ci #[cfg_attr( 295b8a62b91Sopenharmony_ci staged_api, 296b8a62b91Sopenharmony_ci rustc_const_unstable(feature = "const_socketaddr", issue = "82485") 297b8a62b91Sopenharmony_ci )] 298b8a62b91Sopenharmony_ci pub const fn new(ip: Ipv4Addr, port: u16) -> SocketAddrV4 { 299b8a62b91Sopenharmony_ci SocketAddrV4 { ip, port } 300b8a62b91Sopenharmony_ci } 301b8a62b91Sopenharmony_ci 302b8a62b91Sopenharmony_ci /// Returns the IP address associated with this socket address. 303b8a62b91Sopenharmony_ci /// 304b8a62b91Sopenharmony_ci /// # Examples 305b8a62b91Sopenharmony_ci /// 306b8a62b91Sopenharmony_ci /// ``` 307b8a62b91Sopenharmony_ci /// use std::net::{SocketAddrV4, Ipv4Addr}; 308b8a62b91Sopenharmony_ci /// 309b8a62b91Sopenharmony_ci /// let socket = SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 8080); 310b8a62b91Sopenharmony_ci /// assert_eq!(socket.ip(), &Ipv4Addr::new(127, 0, 0, 1)); 311b8a62b91Sopenharmony_ci /// ``` 312b8a62b91Sopenharmony_ci #[must_use] 313b8a62b91Sopenharmony_ci #[cfg_attr(staged_api, stable(feature = "rust1", since = "1.0.0"))] 314b8a62b91Sopenharmony_ci #[cfg_attr( 315b8a62b91Sopenharmony_ci staged_api, 316b8a62b91Sopenharmony_ci rustc_const_unstable(feature = "const_socketaddr", issue = "82485") 317b8a62b91Sopenharmony_ci )] 318b8a62b91Sopenharmony_ci pub const fn ip(&self) -> &Ipv4Addr { 319b8a62b91Sopenharmony_ci &self.ip 320b8a62b91Sopenharmony_ci } 321b8a62b91Sopenharmony_ci 322b8a62b91Sopenharmony_ci /// Changes the IP address associated with this socket address. 323b8a62b91Sopenharmony_ci /// 324b8a62b91Sopenharmony_ci /// # Examples 325b8a62b91Sopenharmony_ci /// 326b8a62b91Sopenharmony_ci /// ``` 327b8a62b91Sopenharmony_ci /// use std::net::{SocketAddrV4, Ipv4Addr}; 328b8a62b91Sopenharmony_ci /// 329b8a62b91Sopenharmony_ci /// let mut socket = SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 8080); 330b8a62b91Sopenharmony_ci /// socket.set_ip(Ipv4Addr::new(192, 168, 0, 1)); 331b8a62b91Sopenharmony_ci /// assert_eq!(socket.ip(), &Ipv4Addr::new(192, 168, 0, 1)); 332b8a62b91Sopenharmony_ci /// ``` 333b8a62b91Sopenharmony_ci #[cfg_attr(staged_api, stable(feature = "sockaddr_setters", since = "1.9.0"))] 334b8a62b91Sopenharmony_ci pub fn set_ip(&mut self, new_ip: Ipv4Addr) { 335b8a62b91Sopenharmony_ci self.ip = new_ip; 336b8a62b91Sopenharmony_ci } 337b8a62b91Sopenharmony_ci 338b8a62b91Sopenharmony_ci /// Returns the port number associated with this socket address. 339b8a62b91Sopenharmony_ci /// 340b8a62b91Sopenharmony_ci /// # Examples 341b8a62b91Sopenharmony_ci /// 342b8a62b91Sopenharmony_ci /// ``` 343b8a62b91Sopenharmony_ci /// use std::net::{SocketAddrV4, Ipv4Addr}; 344b8a62b91Sopenharmony_ci /// 345b8a62b91Sopenharmony_ci /// let socket = SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 8080); 346b8a62b91Sopenharmony_ci /// assert_eq!(socket.port(), 8080); 347b8a62b91Sopenharmony_ci /// ``` 348b8a62b91Sopenharmony_ci #[must_use] 349b8a62b91Sopenharmony_ci #[cfg_attr(staged_api, stable(feature = "rust1", since = "1.0.0"))] 350b8a62b91Sopenharmony_ci #[cfg_attr( 351b8a62b91Sopenharmony_ci staged_api, 352b8a62b91Sopenharmony_ci rustc_const_unstable(feature = "const_socketaddr", issue = "82485") 353b8a62b91Sopenharmony_ci )] 354b8a62b91Sopenharmony_ci pub const fn port(&self) -> u16 { 355b8a62b91Sopenharmony_ci self.port 356b8a62b91Sopenharmony_ci } 357b8a62b91Sopenharmony_ci 358b8a62b91Sopenharmony_ci /// Changes the port number associated with this socket address. 359b8a62b91Sopenharmony_ci /// 360b8a62b91Sopenharmony_ci /// # Examples 361b8a62b91Sopenharmony_ci /// 362b8a62b91Sopenharmony_ci /// ``` 363b8a62b91Sopenharmony_ci /// use std::net::{SocketAddrV4, Ipv4Addr}; 364b8a62b91Sopenharmony_ci /// 365b8a62b91Sopenharmony_ci /// let mut socket = SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 8080); 366b8a62b91Sopenharmony_ci /// socket.set_port(4242); 367b8a62b91Sopenharmony_ci /// assert_eq!(socket.port(), 4242); 368b8a62b91Sopenharmony_ci /// ``` 369b8a62b91Sopenharmony_ci #[cfg_attr(staged_api, stable(feature = "sockaddr_setters", since = "1.9.0"))] 370b8a62b91Sopenharmony_ci pub fn set_port(&mut self, new_port: u16) { 371b8a62b91Sopenharmony_ci self.port = new_port; 372b8a62b91Sopenharmony_ci } 373b8a62b91Sopenharmony_ci} 374b8a62b91Sopenharmony_ci 375b8a62b91Sopenharmony_ciimpl SocketAddrV6 { 376b8a62b91Sopenharmony_ci /// Creates a new socket address from an [`IPv6` address], a 16-bit port number, 377b8a62b91Sopenharmony_ci /// and the `flowinfo` and `scope_id` fields. 378b8a62b91Sopenharmony_ci /// 379b8a62b91Sopenharmony_ci /// For more information on the meaning and layout of the `flowinfo` and `scope_id` 380b8a62b91Sopenharmony_ci /// parameters, see [IETF RFC 2553, Section 3.3]. 381b8a62b91Sopenharmony_ci /// 382b8a62b91Sopenharmony_ci /// [IETF RFC 2553, Section 3.3]: https://tools.ietf.org/html/rfc2553#section-3.3 383b8a62b91Sopenharmony_ci /// [`IPv6` address]: Ipv6Addr 384b8a62b91Sopenharmony_ci /// 385b8a62b91Sopenharmony_ci /// # Examples 386b8a62b91Sopenharmony_ci /// 387b8a62b91Sopenharmony_ci /// ``` 388b8a62b91Sopenharmony_ci /// use std::net::{SocketAddrV6, Ipv6Addr}; 389b8a62b91Sopenharmony_ci /// 390b8a62b91Sopenharmony_ci /// let socket = SocketAddrV6::new(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1), 8080, 0, 0); 391b8a62b91Sopenharmony_ci /// ``` 392b8a62b91Sopenharmony_ci #[cfg_attr(staged_api, stable(feature = "rust1", since = "1.0.0"))] 393b8a62b91Sopenharmony_ci #[must_use] 394b8a62b91Sopenharmony_ci #[cfg_attr( 395b8a62b91Sopenharmony_ci staged_api, 396b8a62b91Sopenharmony_ci rustc_const_unstable(feature = "const_socketaddr", issue = "82485") 397b8a62b91Sopenharmony_ci )] 398b8a62b91Sopenharmony_ci pub const fn new(ip: Ipv6Addr, port: u16, flowinfo: u32, scope_id: u32) -> SocketAddrV6 { 399b8a62b91Sopenharmony_ci SocketAddrV6 { 400b8a62b91Sopenharmony_ci ip, 401b8a62b91Sopenharmony_ci port, 402b8a62b91Sopenharmony_ci flowinfo, 403b8a62b91Sopenharmony_ci scope_id, 404b8a62b91Sopenharmony_ci } 405b8a62b91Sopenharmony_ci } 406b8a62b91Sopenharmony_ci 407b8a62b91Sopenharmony_ci /// Returns the IP address associated with this socket address. 408b8a62b91Sopenharmony_ci /// 409b8a62b91Sopenharmony_ci /// # Examples 410b8a62b91Sopenharmony_ci /// 411b8a62b91Sopenharmony_ci /// ``` 412b8a62b91Sopenharmony_ci /// use std::net::{SocketAddrV6, Ipv6Addr}; 413b8a62b91Sopenharmony_ci /// 414b8a62b91Sopenharmony_ci /// let socket = SocketAddrV6::new(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1), 8080, 0, 0); 415b8a62b91Sopenharmony_ci /// assert_eq!(socket.ip(), &Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1)); 416b8a62b91Sopenharmony_ci /// ``` 417b8a62b91Sopenharmony_ci #[must_use] 418b8a62b91Sopenharmony_ci #[cfg_attr(staged_api, stable(feature = "rust1", since = "1.0.0"))] 419b8a62b91Sopenharmony_ci #[cfg_attr( 420b8a62b91Sopenharmony_ci staged_api, 421b8a62b91Sopenharmony_ci rustc_const_unstable(feature = "const_socketaddr", issue = "82485") 422b8a62b91Sopenharmony_ci )] 423b8a62b91Sopenharmony_ci pub const fn ip(&self) -> &Ipv6Addr { 424b8a62b91Sopenharmony_ci &self.ip 425b8a62b91Sopenharmony_ci } 426b8a62b91Sopenharmony_ci 427b8a62b91Sopenharmony_ci /// Changes the IP address associated with this socket address. 428b8a62b91Sopenharmony_ci /// 429b8a62b91Sopenharmony_ci /// # Examples 430b8a62b91Sopenharmony_ci /// 431b8a62b91Sopenharmony_ci /// ``` 432b8a62b91Sopenharmony_ci /// use std::net::{SocketAddrV6, Ipv6Addr}; 433b8a62b91Sopenharmony_ci /// 434b8a62b91Sopenharmony_ci /// let mut socket = SocketAddrV6::new(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1), 8080, 0, 0); 435b8a62b91Sopenharmony_ci /// socket.set_ip(Ipv6Addr::new(76, 45, 0, 0, 0, 0, 0, 0)); 436b8a62b91Sopenharmony_ci /// assert_eq!(socket.ip(), &Ipv6Addr::new(76, 45, 0, 0, 0, 0, 0, 0)); 437b8a62b91Sopenharmony_ci /// ``` 438b8a62b91Sopenharmony_ci #[cfg_attr(staged_api, stable(feature = "sockaddr_setters", since = "1.9.0"))] 439b8a62b91Sopenharmony_ci pub fn set_ip(&mut self, new_ip: Ipv6Addr) { 440b8a62b91Sopenharmony_ci self.ip = new_ip; 441b8a62b91Sopenharmony_ci } 442b8a62b91Sopenharmony_ci 443b8a62b91Sopenharmony_ci /// Returns the port number associated with this socket address. 444b8a62b91Sopenharmony_ci /// 445b8a62b91Sopenharmony_ci /// # Examples 446b8a62b91Sopenharmony_ci /// 447b8a62b91Sopenharmony_ci /// ``` 448b8a62b91Sopenharmony_ci /// use std::net::{SocketAddrV6, Ipv6Addr}; 449b8a62b91Sopenharmony_ci /// 450b8a62b91Sopenharmony_ci /// let socket = SocketAddrV6::new(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1), 8080, 0, 0); 451b8a62b91Sopenharmony_ci /// assert_eq!(socket.port(), 8080); 452b8a62b91Sopenharmony_ci /// ``` 453b8a62b91Sopenharmony_ci #[must_use] 454b8a62b91Sopenharmony_ci #[cfg_attr(staged_api, stable(feature = "rust1", since = "1.0.0"))] 455b8a62b91Sopenharmony_ci #[cfg_attr( 456b8a62b91Sopenharmony_ci staged_api, 457b8a62b91Sopenharmony_ci rustc_const_unstable(feature = "const_socketaddr", issue = "82485") 458b8a62b91Sopenharmony_ci )] 459b8a62b91Sopenharmony_ci pub const fn port(&self) -> u16 { 460b8a62b91Sopenharmony_ci self.port 461b8a62b91Sopenharmony_ci } 462b8a62b91Sopenharmony_ci 463b8a62b91Sopenharmony_ci /// Changes the port number associated with this socket address. 464b8a62b91Sopenharmony_ci /// 465b8a62b91Sopenharmony_ci /// # Examples 466b8a62b91Sopenharmony_ci /// 467b8a62b91Sopenharmony_ci /// ``` 468b8a62b91Sopenharmony_ci /// use std::net::{SocketAddrV6, Ipv6Addr}; 469b8a62b91Sopenharmony_ci /// 470b8a62b91Sopenharmony_ci /// let mut socket = SocketAddrV6::new(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1), 8080, 0, 0); 471b8a62b91Sopenharmony_ci /// socket.set_port(4242); 472b8a62b91Sopenharmony_ci /// assert_eq!(socket.port(), 4242); 473b8a62b91Sopenharmony_ci /// ``` 474b8a62b91Sopenharmony_ci #[cfg_attr(staged_api, stable(feature = "sockaddr_setters", since = "1.9.0"))] 475b8a62b91Sopenharmony_ci pub fn set_port(&mut self, new_port: u16) { 476b8a62b91Sopenharmony_ci self.port = new_port; 477b8a62b91Sopenharmony_ci } 478b8a62b91Sopenharmony_ci 479b8a62b91Sopenharmony_ci /// Returns the flow information associated with this address. 480b8a62b91Sopenharmony_ci /// 481b8a62b91Sopenharmony_ci /// This information corresponds to the `sin6_flowinfo` field in C's `netinet/in.h`, 482b8a62b91Sopenharmony_ci /// as specified in [IETF RFC 2553, Section 3.3]. 483b8a62b91Sopenharmony_ci /// It combines information about the flow label and the traffic class as specified 484b8a62b91Sopenharmony_ci /// in [IETF RFC 2460], respectively [Section 6] and [Section 7]. 485b8a62b91Sopenharmony_ci /// 486b8a62b91Sopenharmony_ci /// [IETF RFC 2553, Section 3.3]: https://tools.ietf.org/html/rfc2553#section-3.3 487b8a62b91Sopenharmony_ci /// [IETF RFC 2460]: https://tools.ietf.org/html/rfc2460 488b8a62b91Sopenharmony_ci /// [Section 6]: https://tools.ietf.org/html/rfc2460#section-6 489b8a62b91Sopenharmony_ci /// [Section 7]: https://tools.ietf.org/html/rfc2460#section-7 490b8a62b91Sopenharmony_ci /// 491b8a62b91Sopenharmony_ci /// # Examples 492b8a62b91Sopenharmony_ci /// 493b8a62b91Sopenharmony_ci /// ``` 494b8a62b91Sopenharmony_ci /// use std::net::{SocketAddrV6, Ipv6Addr}; 495b8a62b91Sopenharmony_ci /// 496b8a62b91Sopenharmony_ci /// let socket = SocketAddrV6::new(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1), 8080, 10, 0); 497b8a62b91Sopenharmony_ci /// assert_eq!(socket.flowinfo(), 10); 498b8a62b91Sopenharmony_ci /// ``` 499b8a62b91Sopenharmony_ci #[must_use] 500b8a62b91Sopenharmony_ci #[cfg_attr(staged_api, stable(feature = "rust1", since = "1.0.0"))] 501b8a62b91Sopenharmony_ci #[cfg_attr( 502b8a62b91Sopenharmony_ci staged_api, 503b8a62b91Sopenharmony_ci rustc_const_unstable(feature = "const_socketaddr", issue = "82485") 504b8a62b91Sopenharmony_ci )] 505b8a62b91Sopenharmony_ci pub const fn flowinfo(&self) -> u32 { 506b8a62b91Sopenharmony_ci self.flowinfo 507b8a62b91Sopenharmony_ci } 508b8a62b91Sopenharmony_ci 509b8a62b91Sopenharmony_ci /// Changes the flow information associated with this socket address. 510b8a62b91Sopenharmony_ci /// 511b8a62b91Sopenharmony_ci /// See [`SocketAddrV6::flowinfo`]'s documentation for more details. 512b8a62b91Sopenharmony_ci /// 513b8a62b91Sopenharmony_ci /// # Examples 514b8a62b91Sopenharmony_ci /// 515b8a62b91Sopenharmony_ci /// ``` 516b8a62b91Sopenharmony_ci /// use std::net::{SocketAddrV6, Ipv6Addr}; 517b8a62b91Sopenharmony_ci /// 518b8a62b91Sopenharmony_ci /// let mut socket = SocketAddrV6::new(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1), 8080, 10, 0); 519b8a62b91Sopenharmony_ci /// socket.set_flowinfo(56); 520b8a62b91Sopenharmony_ci /// assert_eq!(socket.flowinfo(), 56); 521b8a62b91Sopenharmony_ci /// ``` 522b8a62b91Sopenharmony_ci #[cfg_attr(staged_api, stable(feature = "sockaddr_setters", since = "1.9.0"))] 523b8a62b91Sopenharmony_ci pub fn set_flowinfo(&mut self, new_flowinfo: u32) { 524b8a62b91Sopenharmony_ci self.flowinfo = new_flowinfo; 525b8a62b91Sopenharmony_ci } 526b8a62b91Sopenharmony_ci 527b8a62b91Sopenharmony_ci /// Returns the scope ID associated with this address. 528b8a62b91Sopenharmony_ci /// 529b8a62b91Sopenharmony_ci /// This information corresponds to the `sin6_scope_id` field in C's `netinet/in.h`, 530b8a62b91Sopenharmony_ci /// as specified in [IETF RFC 2553, Section 3.3]. 531b8a62b91Sopenharmony_ci /// 532b8a62b91Sopenharmony_ci /// [IETF RFC 2553, Section 3.3]: https://tools.ietf.org/html/rfc2553#section-3.3 533b8a62b91Sopenharmony_ci /// 534b8a62b91Sopenharmony_ci /// # Examples 535b8a62b91Sopenharmony_ci /// 536b8a62b91Sopenharmony_ci /// ``` 537b8a62b91Sopenharmony_ci /// use std::net::{SocketAddrV6, Ipv6Addr}; 538b8a62b91Sopenharmony_ci /// 539b8a62b91Sopenharmony_ci /// let socket = SocketAddrV6::new(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1), 8080, 0, 78); 540b8a62b91Sopenharmony_ci /// assert_eq!(socket.scope_id(), 78); 541b8a62b91Sopenharmony_ci /// ``` 542b8a62b91Sopenharmony_ci #[must_use] 543b8a62b91Sopenharmony_ci #[cfg_attr(staged_api, stable(feature = "rust1", since = "1.0.0"))] 544b8a62b91Sopenharmony_ci #[cfg_attr( 545b8a62b91Sopenharmony_ci staged_api, 546b8a62b91Sopenharmony_ci rustc_const_unstable(feature = "const_socketaddr", issue = "82485") 547b8a62b91Sopenharmony_ci )] 548b8a62b91Sopenharmony_ci pub const fn scope_id(&self) -> u32 { 549b8a62b91Sopenharmony_ci self.scope_id 550b8a62b91Sopenharmony_ci } 551b8a62b91Sopenharmony_ci 552b8a62b91Sopenharmony_ci /// Changes the scope ID associated with this socket address. 553b8a62b91Sopenharmony_ci /// 554b8a62b91Sopenharmony_ci /// See [`SocketAddrV6::scope_id`]'s documentation for more details. 555b8a62b91Sopenharmony_ci /// 556b8a62b91Sopenharmony_ci /// # Examples 557b8a62b91Sopenharmony_ci /// 558b8a62b91Sopenharmony_ci /// ``` 559b8a62b91Sopenharmony_ci /// use std::net::{SocketAddrV6, Ipv6Addr}; 560b8a62b91Sopenharmony_ci /// 561b8a62b91Sopenharmony_ci /// let mut socket = SocketAddrV6::new(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1), 8080, 0, 78); 562b8a62b91Sopenharmony_ci /// socket.set_scope_id(42); 563b8a62b91Sopenharmony_ci /// assert_eq!(socket.scope_id(), 42); 564b8a62b91Sopenharmony_ci /// ``` 565b8a62b91Sopenharmony_ci #[cfg_attr(staged_api, stable(feature = "sockaddr_setters", since = "1.9.0"))] 566b8a62b91Sopenharmony_ci pub fn set_scope_id(&mut self, new_scope_id: u32) { 567b8a62b91Sopenharmony_ci self.scope_id = new_scope_id; 568b8a62b91Sopenharmony_ci } 569b8a62b91Sopenharmony_ci} 570b8a62b91Sopenharmony_ci 571b8a62b91Sopenharmony_ci#[cfg_attr(staged_api, stable(feature = "ip_from_ip", since = "1.16.0"))] 572b8a62b91Sopenharmony_ciimpl From<SocketAddrV4> for SocketAddr { 573b8a62b91Sopenharmony_ci /// Converts a [`SocketAddrV4`] into a [`SocketAddr::V4`]. 574b8a62b91Sopenharmony_ci fn from(sock4: SocketAddrV4) -> SocketAddr { 575b8a62b91Sopenharmony_ci SocketAddr::V4(sock4) 576b8a62b91Sopenharmony_ci } 577b8a62b91Sopenharmony_ci} 578b8a62b91Sopenharmony_ci 579b8a62b91Sopenharmony_ci#[cfg_attr(staged_api, stable(feature = "ip_from_ip", since = "1.16.0"))] 580b8a62b91Sopenharmony_ciimpl From<SocketAddrV6> for SocketAddr { 581b8a62b91Sopenharmony_ci /// Converts a [`SocketAddrV6`] into a [`SocketAddr::V6`]. 582b8a62b91Sopenharmony_ci fn from(sock6: SocketAddrV6) -> SocketAddr { 583b8a62b91Sopenharmony_ci SocketAddr::V6(sock6) 584b8a62b91Sopenharmony_ci } 585b8a62b91Sopenharmony_ci} 586b8a62b91Sopenharmony_ci 587b8a62b91Sopenharmony_ci#[cfg_attr(staged_api, stable(feature = "addr_from_into_ip", since = "1.17.0"))] 588b8a62b91Sopenharmony_ciimpl<I: Into<IpAddr>> From<(I, u16)> for SocketAddr { 589b8a62b91Sopenharmony_ci /// Converts a tuple struct (Into<[`IpAddr`]>, `u16`) into a [`SocketAddr`]. 590b8a62b91Sopenharmony_ci /// 591b8a62b91Sopenharmony_ci /// This conversion creates a [`SocketAddr::V4`] for an [`IpAddr::V4`] 592b8a62b91Sopenharmony_ci /// and creates a [`SocketAddr::V6`] for an [`IpAddr::V6`]. 593b8a62b91Sopenharmony_ci /// 594b8a62b91Sopenharmony_ci /// `u16` is treated as port of the newly created [`SocketAddr`]. 595b8a62b91Sopenharmony_ci fn from(pieces: (I, u16)) -> SocketAddr { 596b8a62b91Sopenharmony_ci SocketAddr::new(pieces.0.into(), pieces.1) 597b8a62b91Sopenharmony_ci } 598b8a62b91Sopenharmony_ci} 599b8a62b91Sopenharmony_ci 600b8a62b91Sopenharmony_ci#[cfg_attr(staged_api, stable(feature = "socketaddr_ordering", since = "1.45.0"))] 601b8a62b91Sopenharmony_ciimpl PartialOrd for SocketAddrV4 { 602b8a62b91Sopenharmony_ci fn partial_cmp(&self, other: &SocketAddrV4) -> Option<Ordering> { 603b8a62b91Sopenharmony_ci Some(self.cmp(other)) 604b8a62b91Sopenharmony_ci } 605b8a62b91Sopenharmony_ci} 606b8a62b91Sopenharmony_ci 607b8a62b91Sopenharmony_ci#[cfg_attr(staged_api, stable(feature = "socketaddr_ordering", since = "1.45.0"))] 608b8a62b91Sopenharmony_ciimpl PartialOrd for SocketAddrV6 { 609b8a62b91Sopenharmony_ci fn partial_cmp(&self, other: &SocketAddrV6) -> Option<Ordering> { 610b8a62b91Sopenharmony_ci Some(self.cmp(other)) 611b8a62b91Sopenharmony_ci } 612b8a62b91Sopenharmony_ci} 613b8a62b91Sopenharmony_ci 614b8a62b91Sopenharmony_ci#[cfg_attr(staged_api, stable(feature = "socketaddr_ordering", since = "1.45.0"))] 615b8a62b91Sopenharmony_ciimpl Ord for SocketAddrV4 { 616b8a62b91Sopenharmony_ci fn cmp(&self, other: &SocketAddrV4) -> Ordering { 617b8a62b91Sopenharmony_ci self.ip() 618b8a62b91Sopenharmony_ci .cmp(other.ip()) 619b8a62b91Sopenharmony_ci .then(self.port().cmp(&other.port())) 620b8a62b91Sopenharmony_ci } 621b8a62b91Sopenharmony_ci} 622b8a62b91Sopenharmony_ci 623b8a62b91Sopenharmony_ci#[cfg_attr(staged_api, stable(feature = "socketaddr_ordering", since = "1.45.0"))] 624b8a62b91Sopenharmony_ciimpl Ord for SocketAddrV6 { 625b8a62b91Sopenharmony_ci fn cmp(&self, other: &SocketAddrV6) -> Ordering { 626b8a62b91Sopenharmony_ci self.ip() 627b8a62b91Sopenharmony_ci .cmp(other.ip()) 628b8a62b91Sopenharmony_ci .then(self.port().cmp(&other.port())) 629b8a62b91Sopenharmony_ci } 630b8a62b91Sopenharmony_ci} 631b8a62b91Sopenharmony_ci 632b8a62b91Sopenharmony_ci#[cfg_attr(staged_api, stable(feature = "rust1", since = "1.0.0"))] 633b8a62b91Sopenharmony_ciimpl hash::Hash for SocketAddrV4 { 634b8a62b91Sopenharmony_ci fn hash<H: hash::Hasher>(&self, s: &mut H) { 635b8a62b91Sopenharmony_ci (self.port, self.ip).hash(s) 636b8a62b91Sopenharmony_ci } 637b8a62b91Sopenharmony_ci} 638b8a62b91Sopenharmony_ci#[cfg_attr(staged_api, stable(feature = "rust1", since = "1.0.0"))] 639b8a62b91Sopenharmony_ciimpl hash::Hash for SocketAddrV6 { 640b8a62b91Sopenharmony_ci fn hash<H: hash::Hasher>(&self, s: &mut H) { 641b8a62b91Sopenharmony_ci (self.port, &self.ip, self.flowinfo, self.scope_id).hash(s) 642b8a62b91Sopenharmony_ci } 643b8a62b91Sopenharmony_ci} 644