tokio/net/unix/
socketaddr.rs

1use std::fmt;
2use std::path::Path;
3
4/// An address associated with a Tokio Unix socket.
5///
6/// This type is a thin wrapper around [`std::os::unix::net::SocketAddr`]. You
7/// can convert to and from the standard library `SocketAddr` type using the
8/// [`From`] trait.
9#[derive(Clone)]
10pub struct SocketAddr(pub(super) std::os::unix::net::SocketAddr);
11
12impl SocketAddr {
13    /// Returns `true` if the address is unnamed.
14    ///
15    /// Documentation reflected in [`SocketAddr`].
16    ///
17    /// [`SocketAddr`]: std::os::unix::net::SocketAddr
18    pub fn is_unnamed(&self) -> bool {
19        self.0.is_unnamed()
20    }
21
22    /// Returns the contents of this address if it is a `pathname` address.
23    ///
24    /// Documentation reflected in [`SocketAddr`].
25    ///
26    /// [`SocketAddr`]: std::os::unix::net::SocketAddr
27    pub fn as_pathname(&self) -> Option<&Path> {
28        self.0.as_pathname()
29    }
30
31    /// Returns the contents of this address if it is in the abstract namespace.
32    ///
33    /// Documentation reflected in [`SocketAddrExt`].
34    /// The abstract namespace is a Linux-specific feature.
35    ///
36    ///
37    /// [`SocketAddrExt`]: std::os::linux::net::SocketAddrExt
38    #[cfg(any(target_os = "linux", target_os = "android"))]
39    #[cfg_attr(docsrs, doc(cfg(any(target_os = "linux", target_os = "android"))))]
40    pub fn as_abstract_name(&self) -> Option<&[u8]> {
41        #[cfg(target_os = "android")]
42        use std::os::android::net::SocketAddrExt;
43        #[cfg(target_os = "linux")]
44        use std::os::linux::net::SocketAddrExt;
45
46        self.0.as_abstract_name()
47    }
48}
49
50impl fmt::Debug for SocketAddr {
51    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
52        self.0.fmt(fmt)
53    }
54}
55
56impl From<std::os::unix::net::SocketAddr> for SocketAddr {
57    fn from(value: std::os::unix::net::SocketAddr) -> Self {
58        SocketAddr(value)
59    }
60}
61
62impl From<SocketAddr> for std::os::unix::net::SocketAddr {
63    fn from(value: SocketAddr) -> Self {
64        value.0
65    }
66}