use crate::net::ip::{IpAddr, Ipv4Addr, Ipv6Addr};
use core::cmp::Ordering;
use core::hash;
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[cfg_attr(staged_api, stable(feature = "rust1", since = "1.0.0"))]
pub enum SocketAddr {
#[cfg_attr(staged_api, stable(feature = "rust1", since = "1.0.0"))]
V4(#[cfg_attr(staged_api, stable(feature = "rust1", since = "1.0.0"))] SocketAddrV4),
#[cfg_attr(staged_api, stable(feature = "rust1", since = "1.0.0"))]
V6(#[cfg_attr(staged_api, stable(feature = "rust1", since = "1.0.0"))] SocketAddrV6),
}
#[derive(Copy, Clone, Eq, PartialEq)]
#[cfg_attr(staged_api, stable(feature = "rust1", since = "1.0.0"))]
pub struct SocketAddrV4 {
ip: Ipv4Addr,
port: u16,
}
#[derive(Copy, Clone, Eq, PartialEq)]
#[cfg_attr(staged_api, stable(feature = "rust1", since = "1.0.0"))]
pub struct SocketAddrV6 {
ip: Ipv6Addr,
port: u16,
flowinfo: u32,
scope_id: u32,
}
impl SocketAddr {
#[cfg_attr(staged_api, stable(feature = "ip_addr", since = "1.7.0"))]
#[must_use]
#[cfg_attr(
staged_api,
rustc_const_unstable(feature = "const_socketaddr", issue = "82485")
)]
pub const fn new(ip: IpAddr, port: u16) -> SocketAddr {
match ip {
IpAddr::V4(a) => SocketAddr::V4(SocketAddrV4::new(a, port)),
IpAddr::V6(a) => SocketAddr::V6(SocketAddrV6::new(a, port, 0, 0)),
}
}
#[must_use]
#[cfg_attr(staged_api, stable(feature = "ip_addr", since = "1.7.0"))]
#[cfg_attr(
staged_api,
rustc_const_unstable(feature = "const_socketaddr", issue = "82485")
)]
pub const fn ip(&self) -> IpAddr {
match *self {
SocketAddr::V4(ref a) => IpAddr::V4(*a.ip()),
SocketAddr::V6(ref a) => IpAddr::V6(*a.ip()),
}
}
#[cfg_attr(staged_api, stable(feature = "sockaddr_setters", since = "1.9.0"))]
pub fn set_ip(&mut self, new_ip: IpAddr) {
match (self, new_ip) {
(&mut SocketAddr::V4(ref mut a), IpAddr::V4(new_ip)) => a.set_ip(new_ip),
(&mut SocketAddr::V6(ref mut a), IpAddr::V6(new_ip)) => a.set_ip(new_ip),
(self_, new_ip) => *self_ = Self::new(new_ip, self_.port()),
}
}
#[must_use]
#[cfg_attr(staged_api, stable(feature = "rust1", since = "1.0.0"))]
#[cfg_attr(
staged_api,
rustc_const_unstable(feature = "const_socketaddr", issue = "82485")
)]
pub const fn port(&self) -> u16 {
match *self {
SocketAddr::V4(ref a) => a.port(),
SocketAddr::V6(ref a) => a.port(),
}
}
#[cfg_attr(staged_api, stable(feature = "sockaddr_setters", since = "1.9.0"))]
pub fn set_port(&mut self, new_port: u16) {
match *self {
SocketAddr::V4(ref mut a) => a.set_port(new_port),
SocketAddr::V6(ref mut a) => a.set_port(new_port),
}
}
#[must_use]
#[cfg_attr(staged_api, stable(feature = "sockaddr_checker", since = "1.16.0"))]
#[cfg_attr(
staged_api,
rustc_const_unstable(feature = "const_socketaddr", issue = "82485")
)]
pub const fn is_ipv4(&self) -> bool {
matches!(*self, SocketAddr::V4(_))
}
#[must_use]
#[cfg_attr(staged_api, stable(feature = "sockaddr_checker", since = "1.16.0"))]
#[cfg_attr(
staged_api,
rustc_const_unstable(feature = "const_socketaddr", issue = "82485")
)]
pub const fn is_ipv6(&self) -> bool {
matches!(*self, SocketAddr::V6(_))
}
}
impl SocketAddrV4 {
#[cfg_attr(staged_api, stable(feature = "rust1", since = "1.0.0"))]
#[must_use]
#[cfg_attr(
staged_api,
rustc_const_unstable(feature = "const_socketaddr", issue = "82485")
)]
pub const fn new(ip: Ipv4Addr, port: u16) -> SocketAddrV4 {
SocketAddrV4 { ip, port }
}
#[must_use]
#[cfg_attr(staged_api, stable(feature = "rust1", since = "1.0.0"))]
#[cfg_attr(
staged_api,
rustc_const_unstable(feature = "const_socketaddr", issue = "82485")
)]
pub const fn ip(&self) -> &Ipv4Addr {
&self.ip
}
#[cfg_attr(staged_api, stable(feature = "sockaddr_setters", since = "1.9.0"))]
pub fn set_ip(&mut self, new_ip: Ipv4Addr) {
self.ip = new_ip;
}
#[must_use]
#[cfg_attr(staged_api, stable(feature = "rust1", since = "1.0.0"))]
#[cfg_attr(
staged_api,
rustc_const_unstable(feature = "const_socketaddr", issue = "82485")
)]
pub const fn port(&self) -> u16 {
self.port
}
#[cfg_attr(staged_api, stable(feature = "sockaddr_setters", since = "1.9.0"))]
pub fn set_port(&mut self, new_port: u16) {
self.port = new_port;
}
}
impl SocketAddrV6 {
#[cfg_attr(staged_api, stable(feature = "rust1", since = "1.0.0"))]
#[must_use]
#[cfg_attr(
staged_api,
rustc_const_unstable(feature = "const_socketaddr", issue = "82485")
)]
pub const fn new(ip: Ipv6Addr, port: u16, flowinfo: u32, scope_id: u32) -> SocketAddrV6 {
SocketAddrV6 {
ip,
port,
flowinfo,
scope_id,
}
}
#[must_use]
#[cfg_attr(staged_api, stable(feature = "rust1", since = "1.0.0"))]
#[cfg_attr(
staged_api,
rustc_const_unstable(feature = "const_socketaddr", issue = "82485")
)]
pub const fn ip(&self) -> &Ipv6Addr {
&self.ip
}
#[cfg_attr(staged_api, stable(feature = "sockaddr_setters", since = "1.9.0"))]
pub fn set_ip(&mut self, new_ip: Ipv6Addr) {
self.ip = new_ip;
}
#[must_use]
#[cfg_attr(staged_api, stable(feature = "rust1", since = "1.0.0"))]
#[cfg_attr(
staged_api,
rustc_const_unstable(feature = "const_socketaddr", issue = "82485")
)]
pub const fn port(&self) -> u16 {
self.port
}
#[cfg_attr(staged_api, stable(feature = "sockaddr_setters", since = "1.9.0"))]
pub fn set_port(&mut self, new_port: u16) {
self.port = new_port;
}
#[must_use]
#[cfg_attr(staged_api, stable(feature = "rust1", since = "1.0.0"))]
#[cfg_attr(
staged_api,
rustc_const_unstable(feature = "const_socketaddr", issue = "82485")
)]
pub const fn flowinfo(&self) -> u32 {
self.flowinfo
}
#[cfg_attr(staged_api, stable(feature = "sockaddr_setters", since = "1.9.0"))]
pub fn set_flowinfo(&mut self, new_flowinfo: u32) {
self.flowinfo = new_flowinfo;
}
#[must_use]
#[cfg_attr(staged_api, stable(feature = "rust1", since = "1.0.0"))]
#[cfg_attr(
staged_api,
rustc_const_unstable(feature = "const_socketaddr", issue = "82485")
)]
pub const fn scope_id(&self) -> u32 {
self.scope_id
}
#[cfg_attr(staged_api, stable(feature = "sockaddr_setters", since = "1.9.0"))]
pub fn set_scope_id(&mut self, new_scope_id: u32) {
self.scope_id = new_scope_id;
}
}
#[cfg_attr(staged_api, stable(feature = "ip_from_ip", since = "1.16.0"))]
impl From<SocketAddrV4> for SocketAddr {
fn from(sock4: SocketAddrV4) -> SocketAddr {
SocketAddr::V4(sock4)
}
}
#[cfg_attr(staged_api, stable(feature = "ip_from_ip", since = "1.16.0"))]
impl From<SocketAddrV6> for SocketAddr {
fn from(sock6: SocketAddrV6) -> SocketAddr {
SocketAddr::V6(sock6)
}
}
#[cfg_attr(staged_api, stable(feature = "addr_from_into_ip", since = "1.17.0"))]
impl<I: Into<IpAddr>> From<(I, u16)> for SocketAddr {
fn from(pieces: (I, u16)) -> SocketAddr {
SocketAddr::new(pieces.0.into(), pieces.1)
}
}
#[cfg_attr(staged_api, stable(feature = "socketaddr_ordering", since = "1.45.0"))]
impl PartialOrd for SocketAddrV4 {
fn partial_cmp(&self, other: &SocketAddrV4) -> Option<Ordering> {
Some(self.cmp(other))
}
}
#[cfg_attr(staged_api, stable(feature = "socketaddr_ordering", since = "1.45.0"))]
impl PartialOrd for SocketAddrV6 {
fn partial_cmp(&self, other: &SocketAddrV6) -> Option<Ordering> {
Some(self.cmp(other))
}
}
#[cfg_attr(staged_api, stable(feature = "socketaddr_ordering", since = "1.45.0"))]
impl Ord for SocketAddrV4 {
fn cmp(&self, other: &SocketAddrV4) -> Ordering {
self.ip()
.cmp(other.ip())
.then(self.port().cmp(&other.port()))
}
}
#[cfg_attr(staged_api, stable(feature = "socketaddr_ordering", since = "1.45.0"))]
impl Ord for SocketAddrV6 {
fn cmp(&self, other: &SocketAddrV6) -> Ordering {
self.ip()
.cmp(other.ip())
.then(self.port().cmp(&other.port()))
}
}
#[cfg_attr(staged_api, stable(feature = "rust1", since = "1.0.0"))]
impl hash::Hash for SocketAddrV4 {
fn hash<H: hash::Hasher>(&self, s: &mut H) {
(self.port, self.ip).hash(s)
}
}
#[cfg_attr(staged_api, stable(feature = "rust1", since = "1.0.0"))]
impl hash::Hash for SocketAddrV6 {
fn hash<H: hash::Hasher>(&self, s: &mut H) {
(self.port, &self.ip, self.flowinfo, self.scope_id).hash(s)
}
}