]> git.lizzy.rs Git - rust.git/blob - library/std/src/os/net/linux_ext/addr.rs
Auto merge of #105650 - cassaundra:float-literal-suggestion, r=pnkfelix
[rust.git] / library / std / src / os / net / linux_ext / addr.rs
1 //! Linux and Android-specific extensions to socket addresses.
2
3 use crate::os::unix::net::SocketAddr;
4 use crate::sealed::Sealed;
5
6 /// Platform-specific extensions to [`SocketAddr`].
7 #[unstable(feature = "unix_socket_abstract", issue = "85410")]
8 pub trait SocketAddrExt: Sealed {
9     /// Creates a Unix socket address in the abstract namespace.
10     ///
11     /// The abstract namespace is a Linux-specific extension that allows Unix
12     /// sockets to be bound without creating an entry in the filesystem.
13     /// Abstract sockets are unaffected by filesystem layout or permissions,
14     /// and no cleanup is necessary when the socket is closed.
15     ///
16     /// An abstract socket address name may contain any bytes, including zero.
17     ///
18     /// # Errors
19     ///
20     /// Returns an error if the name is longer than `SUN_LEN - 1`.
21     ///
22     /// # Examples
23     ///
24     /// ```no_run
25     /// #![feature(unix_socket_abstract)]
26     /// use std::os::unix::net::{UnixListener, SocketAddr};
27     /// use std::os::linux::net::SocketAddrExt;
28     ///
29     /// fn main() -> std::io::Result<()> {
30     ///     let addr = SocketAddr::from_abstract_name(b"hidden")?;
31     ///     let listener = match UnixListener::bind_addr(&addr) {
32     ///         Ok(sock) => sock,
33     ///         Err(err) => {
34     ///             println!("Couldn't bind: {err:?}");
35     ///             return Err(err);
36     ///         }
37     ///     };
38     ///     Ok(())
39     /// }
40     /// ```
41     fn from_abstract_name<N>(name: N) -> crate::io::Result<SocketAddr>
42     where
43         N: AsRef<[u8]>;
44
45     /// Returns the contents of this address if it is in the abstract namespace.
46     ///
47     /// # Examples
48     ///
49     /// ```no_run
50     /// #![feature(unix_socket_abstract)]
51     /// use std::os::unix::net::{UnixListener, SocketAddr};
52     /// use std::os::linux::net::SocketAddrExt;
53     ///
54     /// fn main() -> std::io::Result<()> {
55     ///     let name = b"hidden";
56     ///     let name_addr = SocketAddr::from_abstract_name(name)?;
57     ///     let socket = UnixListener::bind_addr(&name_addr)?;
58     ///     let local_addr = socket.local_addr().expect("Couldn't get local address");
59     ///     assert_eq!(local_addr.as_abstract_name(), Some(&name[..]));
60     ///     Ok(())
61     /// }
62     /// ```
63     fn as_abstract_name(&self) -> Option<&[u8]>;
64 }