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
82
83
84
85
86
87
88
#[allow(unused_imports)]
use std::{fmt::Display, net::SocketAddr, sync::Arc};

/// Some form of accepted connection's address.
/// Variant depends on variant used in [`ListenerAddress`].
#[derive(Debug)]
#[non_exhaustive]
#[allow(missing_docs)]
pub enum SomeSocketAddr {
    Tcp(SocketAddr),
    #[cfg(all(feature = "unix", unix))]
    #[cfg_attr(docsrs_alt, doc(cfg(all(feature = "unix", unix))))]
    Unix(tokio::net::unix::SocketAddr),
    #[cfg(feature = "inetd")]
    #[cfg_attr(docsrs_alt, doc(cfg(feature = "inetd")))]
    Stdio,
    #[cfg(feature = "multi-listener")]
    #[cfg_attr(docsrs_alt, doc(cfg(feature = "multi-listener")))]
    Multiple,
}

impl Display for SomeSocketAddr {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            SomeSocketAddr::Tcp(x) => x.fmt(f),
            #[cfg(all(feature = "unix", unix))]
            SomeSocketAddr::Unix(_x) => "unix".fmt(f),
            #[cfg(feature = "inetd")]
            SomeSocketAddr::Stdio => "stdio".fmt(f),
            #[cfg(feature = "multi-listener")]
            SomeSocketAddr::Multiple => "multiple".fmt(f),
        }
    }
}

impl SomeSocketAddr {
    /// Convert this address representation into a clonable form.
    /// For UNIX socket addresses, it converts them to a string using Debug representation.
    #[must_use]
    pub fn clonable(self) -> SomeSocketAddrClonable {
        match self {
            SomeSocketAddr::Tcp(x) => SomeSocketAddrClonable::Tcp(x),
            #[cfg(all(feature = "unix", unix))]
            SomeSocketAddr::Unix(x) => SomeSocketAddrClonable::Unix(Arc::new(x)),
            #[cfg(feature = "inetd")]
            SomeSocketAddr::Stdio => SomeSocketAddrClonable::Stdio,
            #[cfg(feature = "multi-listener")]
            SomeSocketAddr::Multiple => SomeSocketAddrClonable::Multiple,
        }
    }
}

/// Other representation of [`SomeSocketAddr`] with Arc-wrapped Unix addresses to enable cloning
#[derive(Debug, Clone)]
#[non_exhaustive]
#[allow(missing_docs)]
pub enum SomeSocketAddrClonable {
    Tcp(SocketAddr),
    #[cfg(all(feature = "unix", unix))]
    #[cfg_attr(docsrs_alt, doc(cfg(all(feature = "unix", unix))))]
    Unix(Arc<tokio::net::unix::SocketAddr>),
    #[cfg(feature = "inetd")]
    #[cfg_attr(docsrs_alt, doc(cfg(feature = "inetd")))]
    Stdio,
    #[cfg(feature = "multi-listener")]
    #[cfg_attr(docsrs_alt, doc(cfg(feature = "multi-listener")))]
    Multiple,
}

impl Display for SomeSocketAddrClonable {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            SomeSocketAddrClonable::Tcp(x) => x.fmt(f),
            #[cfg(all(feature = "unix", unix))]
            SomeSocketAddrClonable::Unix(x) => write!(f, "unix:{x:?}"),
            #[cfg(feature = "inetd")]
            SomeSocketAddrClonable::Stdio => "stdio".fmt(f),
            #[cfg(feature = "multi-listener")]
            SomeSocketAddrClonable::Multiple => "multiple".fmt(f),
        }
    }
}

impl From<SomeSocketAddr> for SomeSocketAddrClonable {
    fn from(value: SomeSocketAddr) -> Self {
        value.clonable()
    }
}