interprocess_docfix/local_socket/tokio/stream/
mod.rs

1mod read_half;
2pub use read_half::*;
3
4mod write_half;
5pub use write_half::*;
6
7use {
8    super::super::ToLocalSocketName,
9    futures_io::{AsyncRead, AsyncWrite},
10    std::{
11        fmt::{self, Debug, Formatter},
12        io::{self, IoSlice, IoSliceMut},
13        pin::Pin,
14        task::{Context, Poll},
15    },
16};
17
18#[cfg(feature = "tokio_support")]
19impmod! {local_socket::tokio,
20    LocalSocketStream as LocalSocketStreamImpl
21}
22#[cfg(not(feature = "tokio_support"))]
23struct LocalSocketStreamImpl;
24
25/// A Tokio-based local socket byte stream, obtained eiter from [`LocalSocketListener`] or by connecting to an existing local socket.
26///
27/// # Examples
28/// - [Basic client](https://github.com/kotauskas/interprocess/blob/main/examples/tokio_local_socket/client.rs)
29///
30/// [`LocalSocketListener`]: struct.LocalSocketListener.html " "
31pub struct LocalSocketStream {
32    pub(super) inner: LocalSocketStreamImpl,
33}
34impl LocalSocketStream {
35    /// Connects to a remote local socket server.
36    pub async fn connect<'a>(name: impl ToLocalSocketName<'a>) -> io::Result<Self> {
37        Ok(Self {
38            inner: LocalSocketStreamImpl::connect(name).await?,
39        })
40    }
41    /// Splits a stream into a read half and a write half, which can be used to read and write the stream concurrently.
42    pub fn into_split(self) -> (OwnedReadHalf, OwnedWriteHalf) {
43        let (r, w) = self.inner.into_split();
44        (OwnedReadHalf { inner: r }, OwnedWriteHalf { inner: w })
45    }
46    /// Retrieves the identifier of the process on the opposite end of the local socket connection.
47    ///
48    /// # Platform-specific behavior
49    /// ## macOS and iOS
50    /// Not supported by the OS, will always generate an error at runtime.
51    pub fn peer_pid(&self) -> io::Result<u32> {
52        self.inner.peer_pid()
53    }
54    fn pinproj(&mut self) -> Pin<&mut LocalSocketStreamImpl> {
55        Pin::new(&mut self.inner)
56    }
57}
58
59impl AsyncRead for LocalSocketStream {
60    fn poll_read(
61        mut self: Pin<&mut Self>,
62        cx: &mut Context<'_>,
63        buf: &mut [u8],
64    ) -> Poll<io::Result<usize>> {
65        self.pinproj().poll_read(cx, buf)
66    }
67    fn poll_read_vectored(
68        mut self: Pin<&mut Self>,
69        cx: &mut Context<'_>,
70        bufs: &mut [IoSliceMut<'_>],
71    ) -> Poll<io::Result<usize>> {
72        self.pinproj().poll_read_vectored(cx, bufs)
73    }
74}
75impl AsyncWrite for LocalSocketStream {
76    fn poll_write(
77        mut self: Pin<&mut Self>,
78        cx: &mut Context<'_>,
79        buf: &[u8],
80    ) -> Poll<io::Result<usize>> {
81        self.pinproj().poll_write(cx, buf)
82    }
83    fn poll_write_vectored(
84        mut self: Pin<&mut Self>,
85        cx: &mut Context<'_>,
86        bufs: &[IoSlice<'_>],
87    ) -> Poll<io::Result<usize>> {
88        self.pinproj().poll_write_vectored(cx, bufs)
89    }
90    // Those don't do anything
91    fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
92        self.pinproj().poll_flush(cx)
93    }
94    fn poll_close(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
95        self.pinproj().poll_close(cx)
96    }
97}
98
99impl Debug for LocalSocketStream {
100    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
101        Debug::fmt(&self.inner, f)
102    }
103}
104
105impl_as_raw_handle!(LocalSocketStream);