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) -> &StdSocketAddr
pub const fn as_inner(&self) -> &StdSocketAddr
Returns a reference to the inner value.
Source§impl SocketAddr
impl SocketAddr
Sourcepub const fn from(inner: StdSocketAddr) -> Self
pub const fn from(inner: StdSocketAddr) -> 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 unix SocketAddr
from its string representation.
§Address Types
- Strings starting with
@
or\0
are 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.
§Important
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::from_bytes_until_nul
to ensure only the portion
before the first null byte is used for address parsing.
§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();
Sourcepub fn new_abstract(bytes: &[u8]) -> Result<Self>
pub fn new_abstract(bytes: &[u8]) -> Result<Self>
Creates a new abstract unix SocketAddr
.
Sourcepub fn new_pathname<P: AsRef<Path>>(pathname: P) -> Result<Self>
pub fn new_pathname<P: AsRef<Path>>(pathname: P) -> Result<Self>
Creates a new pathname unix SocketAddr
.
Sourcepub fn new_unnamed() -> Self
pub fn new_unnamed() -> Self
Creates a new unnamed unix SocketAddr
.
Sourcepub fn from_bytes(bytes: &[u8]) -> Result<Self>
pub fn from_bytes(bytes: &[u8]) -> Result<Self>
Creates a new unix SocketAddr
from bytes.
§Note
This method does not validate null terminators. Pathname addresses will reject paths containing null bytes during parsing, but abstract addresses accept null bytes silently, which may lead to unexpected behavior.
Consider using from_bytes_until_nul
for null-terminated parsing.
Sourcepub fn from_bytes_until_nul(bytes: &[u8]) -> Result<Self>
pub fn from_bytes_until_nul(bytes: &[u8]) -> Result<Self>
Creates a new unix SocketAddr
from bytes until the first null byte.
Sourcepub fn bind_std(&self) -> Result<UnixListener>
pub fn bind_std(&self) -> Result<UnixListener>
Binds to the Unix socket address and creates a
std::os::unix::net::UnixListener
.
Sourcepub fn bind(&self) -> Result<UnixListener>
pub fn bind(&self) -> Result<UnixListener>
Binds to the Unix socket address and creates a
tokio::net::UnixListener
.
Sourcepub fn connect_std(&self) -> Result<UnixStream>
pub fn connect_std(&self) -> Result<UnixStream>
Connects to the Unix socket address and returns a
std::os::unix::net::UnixStream
.
Sourcepub fn connect(&self) -> Result<UnixStream>
pub fn connect(&self) -> Result<UnixStream>
Connects to the Unix socket address and returns a
tokio::net::UnixStream
.
Sourcepub fn to_os_string(&self) -> OsString
pub fn to_os_string(&self) -> OsString
Serializes the Unix socket address 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_ext(&self) -> Option<String>
pub fn to_string_ext(&self) -> Option<String>
Likes to_os_string
, but returns a String
instead of OsString
, performing UTF-8 verification.
§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 = StdSocketAddr>§
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) -> &StdSocketAddr
fn as_ref(&self) -> &StdSocketAddr
Source§impl Borrow<SocketAddr> for SocketAddr
impl Borrow<SocketAddr> for SocketAddr
Source§fn borrow(&self) -> &StdSocketAddr
fn borrow(&self) -> &StdSocketAddr
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 more