rama_unix/unix/
mod.rs

1mod address;
2use std::ops::{Deref, DerefMut};
3
4pub use address::UnixSocketAddress;
5
6pub mod client;
7pub mod server;
8
9mod frame;
10#[doc(inline)]
11pub use frame::UnixDatagramFramed;
12
13pub mod codec {
14    //! Adaptors from `AsyncRead`/`AsyncWrite` to Stream/Sink
15    //!
16    //! Raw I/O objects work with byte sequences, but higher-level code usually
17    //! wants to batch these into meaningful chunks, called "frames".
18    //!
19    //! Re-export of [`tokio_util::codec`].
20
21    pub use tokio_util::codec::*;
22}
23
24pub use tokio::net::unix::SocketAddr as TokioSocketAddress;
25pub use tokio::net::{UnixDatagram, UnixSocket, UnixStream};
26
27#[derive(Debug, Clone)]
28/// Information about the socket on the egress end.
29pub struct ClientUnixSocketInfo(pub UnixSocketInfo);
30
31impl AsRef<UnixSocketInfo> for ClientUnixSocketInfo {
32    fn as_ref(&self) -> &UnixSocketInfo {
33        &self.0
34    }
35}
36
37impl AsMut<UnixSocketInfo> for ClientUnixSocketInfo {
38    fn as_mut(&mut self) -> &mut UnixSocketInfo {
39        &mut self.0
40    }
41}
42
43impl Deref for ClientUnixSocketInfo {
44    type Target = UnixSocketInfo;
45
46    fn deref(&self) -> &Self::Target {
47        &self.0
48    }
49}
50impl DerefMut for ClientUnixSocketInfo {
51    fn deref_mut(&mut self) -> &mut Self::Target {
52        &mut self.0
53    }
54}
55
56#[derive(Debug, Clone)]
57/// Connected unix socket information.
58pub struct UnixSocketInfo {
59    local_addr: Option<UnixSocketAddress>,
60    peer_addr: UnixSocketAddress,
61}
62
63impl UnixSocketInfo {
64    /// Create a new [`UnixSocketInfo`].
65    pub fn new(
66        local_addr: Option<impl Into<UnixSocketAddress>>,
67        peer_addr: impl Into<UnixSocketAddress>,
68    ) -> Self {
69        Self {
70            local_addr: local_addr.map(Into::into),
71            peer_addr: peer_addr.into(),
72        }
73    }
74
75    /// Try to get the address of the local unix (domain) socket.
76    #[must_use]
77    pub fn local_addr(&self) -> Option<&UnixSocketAddress> {
78        self.local_addr.as_ref()
79    }
80
81    /// Get the address of the peer unix (domain) socket.
82    #[must_use]
83    pub fn peer_addr(&self) -> &UnixSocketAddress {
84        &self.peer_addr
85    }
86}