1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#![allow(unsafe_code)]
#[cfg(unix)]
use crate::net::SocketAddrUnix;
use crate::net::{AddressFamily, SocketAddrV4, SocketAddrV6};
use crate::{backend, io};
#[cfg(feature = "std")]
use core::fmt;
pub use backend::net::addr::SocketAddrStorage;
#[derive(Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
#[doc(alias = "sockaddr")]
#[non_exhaustive]
pub enum SocketAddrAny {
V4(SocketAddrV4),
V6(SocketAddrV6),
#[cfg(unix)]
Unix(SocketAddrUnix),
}
impl SocketAddrAny {
#[inline]
pub const fn address_family(&self) -> AddressFamily {
match self {
Self::V4(_) => AddressFamily::INET,
Self::V6(_) => AddressFamily::INET6,
#[cfg(unix)]
Self::Unix(_) => AddressFamily::UNIX,
}
}
pub unsafe fn write(&self, storage: *mut SocketAddrStorage) -> usize {
backend::net::write_sockaddr::write_sockaddr(self, storage)
}
pub unsafe fn read(storage: *const SocketAddrStorage, len: usize) -> io::Result<Self> {
backend::net::read_sockaddr::read_sockaddr(storage, len)
}
}
#[cfg(feature = "std")]
impl fmt::Debug for SocketAddrAny {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::V4(v4) => v4.fmt(fmt),
Self::V6(v6) => v6.fmt(fmt),
#[cfg(unix)]
Self::Unix(unix) => unix.fmt(fmt),
}
}
}