pub struct SocketAddr { /* private fields */ }Expand description
Wrapper over std::os::unix::net::SocketAddr.
See SocketAddr::new for more details.
Implementations§
Source§impl SocketAddr
impl SocketAddr
Sourcepub const fn as_inner(&self) -> &SocketAddr
pub const fn as_inner(&self) -> &SocketAddr
Returns a reference to the inner value.
Source§impl SocketAddr
impl SocketAddr
Sourcepub const fn from(inner: SocketAddr) -> Self
pub const fn from(inner: SocketAddr) -> Self
Creates a new instance of the wrapper type from the inner value.
Source§impl SocketAddr
impl SocketAddr
Sourcepub fn new<S: AsRef<OsStr> + ?Sized>(addr: &S) -> Result<Self>
pub fn new<S: AsRef<OsStr> + ?Sized>(addr: &S) -> Result<Self>
Creates a new SocketAddr from its string representation.
§Address Types
- Strings starting with
@or\0are parsed as abstract unix socket addresses (Linux-specific). - All other strings are parsed as pathname unix socket addresses.
- Empty strings create unnamed unix socket addresses.
§Notes
This method accepts an OsStr and does not guarantee proper null
termination. While pathname addresses reject interior null bytes,
abstract addresses accept them silently, potentially causing unexpected
behavior (e.g., \0abstract differs from \0abstract\0\0\0\0\0...).
Use SocketAddr::new_strict to ensure the abstract names do not
contain null bytes, too.
§Examples
#[cfg(any(target_os = "android", target_os = "linux"))]
// Abstract address (Linux-specific)
let abstract_addr = SocketAddr::new("@abstract.example.socket").unwrap();
// Pathname address
let pathname_addr = SocketAddr::new("/run/pathname.example.socket").unwrap();
// Unnamed address
let unnamed_addr = SocketAddr::new("").unwrap();§Errors
Returns an error if the address is invalid or unsupported on the current platform.
See SocketAddr::from_abstract_name
and [StdSocketAddr::from_pathname] for more details.
Sourcepub fn new_strict<S: AsRef<OsStr> + ?Sized>(addr: &S) -> Result<Self>
pub fn new_strict<S: AsRef<OsStr> + ?Sized>(addr: &S) -> Result<Self>
See SocketAddr::new.
Sourcepub fn new_abstract(bytes: &[u8]) -> Result<Self>
pub fn new_abstract(bytes: &[u8]) -> Result<Self>
Creates a Unix socket address in the abstract namespace.
The abstract namespace is a Linux-specific extension that allows Unix sockets to be bound without creating an entry in the filesystem. Abstract sockets are unaffected by filesystem layout or permissions, and no cleanup is necessary when the socket is closed.
An abstract socket address name may contain any bytes, including zero.
However, we don’t recommend using zero bytes, as they may lead to
unexpected behavior. To avoid this, consider using
new_abstract_strict.
§Errors
Returns an error if the name is longer than SUN_LEN - 1.
Sourcepub fn new_abstract_strict(bytes: &[u8]) -> Result<Self>
pub fn new_abstract_strict(bytes: &[u8]) -> Result<Self>
Sourcepub fn new_pathname<P: AsRef<Path>>(pathname: P) -> Result<Self>
pub fn new_pathname<P: AsRef<Path>>(pathname: P) -> Result<Self>
Constructs a SocketAddr with the family AF_UNIX and the provided
path.
§Errors
Returns an error if the path is longer than SUN_LEN or if it contains
NULL bytes.
Sourcepub fn new_unnamed() -> Self
pub fn new_unnamed() -> Self
Creates an unnamed SocketAddr.
Sourcepub fn from_bytes(bytes: &[u8]) -> Result<Self>
pub fn from_bytes(bytes: &[u8]) -> Result<Self>
Sourcepub fn to_os_string(&self) -> OsString
pub fn to_os_string(&self) -> OsString
Serializes the SocketAddr to an OsString.
§Returns
- For abstract ones: returns the name prefixed with
\0 - For pathname ones: returns the pathname
- For unnamed ones: returns an empty string.
Sourcepub fn to_string_lossy(&self) -> String
pub fn to_string_lossy(&self) -> String
Likes to_os_string, but returns a String
instead of OsString, performing lossy UTF-8 conversion.
§Returns
- For abstract ones: returns the name prefixed with
@ - For pathname ones: returns the pathname
- For unnamed ones: returns an empty string.
Methods from Deref<Target = SocketAddr>§
1.10.0 · Sourcepub fn is_unnamed(&self) -> bool
pub fn is_unnamed(&self) -> bool
Returns true if the address is unnamed.
§Examples
A named address:
use std::os::unix::net::UnixListener;
fn main() -> std::io::Result<()> {
let socket = UnixListener::bind("/tmp/sock")?;
let addr = socket.local_addr().expect("Couldn't get local address");
assert_eq!(addr.is_unnamed(), false);
Ok(())
}An unnamed address:
use std::os::unix::net::UnixDatagram;
fn main() -> std::io::Result<()> {
let socket = UnixDatagram::unbound()?;
let addr = socket.local_addr().expect("Couldn't get local address");
assert_eq!(addr.is_unnamed(), true);
Ok(())
}1.10.0 · Sourcepub fn as_pathname(&self) -> Option<&Path>
pub fn as_pathname(&self) -> Option<&Path>
Returns the contents of this address if it is a pathname address.
§Examples
With a pathname:
use std::os::unix::net::UnixListener;
use std::path::Path;
fn main() -> std::io::Result<()> {
let socket = UnixListener::bind("/tmp/sock")?;
let addr = socket.local_addr().expect("Couldn't get local address");
assert_eq!(addr.as_pathname(), Some(Path::new("/tmp/sock")));
Ok(())
}Without a pathname:
use std::os::unix::net::UnixDatagram;
fn main() -> std::io::Result<()> {
let socket = UnixDatagram::unbound()?;
let addr = socket.local_addr().expect("Couldn't get local address");
assert_eq!(addr.as_pathname(), None);
Ok(())
}Trait Implementations§
Source§impl AsRef<SocketAddr> for SocketAddr
impl AsRef<SocketAddr> for SocketAddr
Source§fn as_ref(&self) -> &SocketAddr
fn as_ref(&self) -> &SocketAddr
Source§impl Borrow<SocketAddr> for SocketAddr
impl Borrow<SocketAddr> for SocketAddr
Source§fn borrow(&self) -> &SocketAddr
fn borrow(&self) -> &SocketAddr
Source§impl Clone for SocketAddr
impl Clone for SocketAddr
Source§fn clone(&self) -> SocketAddr
fn clone(&self) -> SocketAddr
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for SocketAddr
impl Debug for SocketAddr
Source§impl Deref for SocketAddr
impl Deref for SocketAddr
Source§impl<'de> Deserialize<'de> for SocketAddr
Available on crate feature feat-serde only.
impl<'de> Deserialize<'de> for SocketAddr
feat-serde only.Source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
Source§impl From<SocketAddr> for SocketAddr
impl From<SocketAddr> for SocketAddr
Source§fn from(inner: SocketAddr) -> Self
fn from(inner: SocketAddr) -> Self
Source§impl From<SocketAddr> for UniAddr
Available on Unix only.
impl From<SocketAddr> for UniAddr
Source§fn from(addr: SocketAddr) -> Self
fn from(addr: SocketAddr) -> Self
Source§impl Hash for SocketAddr
impl Hash for SocketAddr
Source§impl PartialEq for SocketAddr
impl PartialEq for SocketAddr
Source§impl Serialize for SocketAddr
Available on crate feature feat-serde only.
impl Serialize for SocketAddr
feat-serde only.