diff --git a/util/src/mio/mio.rs b/util/src/mio/mio.rs index a228e80..d8bdc70 100644 --- a/util/src/mio/mio.rs +++ b/util/src/mio/mio.rs @@ -21,6 +21,44 @@ pub mod interest { } /// Extension trait providing additional functionality for Unix listener +/// +/// # Example +/// +/// ```rust +/// use mio::net::{UnixListener, UnixStream}; +/// use rosenpass_util::mio::{UnixListenerExt, UnixStreamExt}; +/// +/// use std::os::unix::io::{AsRawFd, IntoRawFd, RawFd}; +/// use std::path::Path; +/// +/// // This would be the UDS created by an external source +/// let socket_path = "/tmp/rp_mio_uds_test_socket"; +/// if Path::new(socket_path).exists() { +/// std::fs::remove_file(socket_path).expect("Failed to remove existing socket"); +/// } +/// +/// // An extended MIO listener can then be created by claiming the existing socket +/// // Note that the original descriptor is not reused, but copied before claiming it here +/// let listener = UnixListener::bind(socket_path).unwrap(); +/// let listener_fd: RawFd = listener.as_raw_fd(); +/// let ext_listener = +/// ::claim_fd(listener_fd).expect("Failed to claim_fd for ext_listener socket"); +/// +/// // Similarly, "client" connections can be established by claiming existing sockets +/// // Note that in this case, the file descriptor will be reused (safety implications!) +/// let stream = UnixStream::connect(socket_path).unwrap(); +/// let stream_fd = stream.into_raw_fd(); +/// let ext_stream = +/// ::claim_fd_inplace(stream_fd).expect("Failed to claim_fd_inplace for ext_stream socket"); +/// +/// // Handle accepted connections... +/// ext_listener.accept().expect("Failed to accept incoming connection"); +/// +/// // Send or receive messages ... +/// +/// // Cleanup, shutdown etc. goes here ... +/// std::fs::remove_file(socket_path).unwrap(); +/// ``` pub trait UnixListenerExt: Sized { /// Creates a new Unix listener by claiming ownership of a raw file descriptor fn claim_fd(fd: RawFd) -> anyhow::Result;