interprocess_docfix/local_socket/tokio/stream/
write_half.rs

1use {
2    futures_io::AsyncWrite,
3    std::{
4        fmt::{self, Debug, Formatter},
5        io::{self, IoSlice},
6        pin::Pin,
7        task::{Context, Poll},
8    },
9};
10
11#[cfg(feature = "tokio_support")]
12impmod! {local_socket::tokio,
13    OwnedWriteHalf as OwnedWriteHalfImpl
14}
15#[cfg(not(feature = "tokio_support"))]
16struct OwnedWriteHalfImpl;
17
18/// An owned write half of a Tokio-based local socket stream, obtained by splitting a [`LocalSocketStream`].
19///
20/// # Examples
21/// - [Basic client](https://github.com/kotauskas/interprocess/blob/main/examples/tokio_local_socket/client.rs)
22///
23/// [`LocalSocketStream`]: struct.LocalSocketStream.html " "
24pub struct OwnedWriteHalf {
25    pub(super) inner: OwnedWriteHalfImpl,
26}
27impl OwnedWriteHalf {
28    /// Retrieves the identifier of the process on the opposite end of the local socket connection.
29    ///
30    /// # Platform-specific behavior
31    /// ## macOS and iOS
32    /// Not supported by the OS, will always generate an error at runtime.
33    pub fn peer_pid(&self) -> io::Result<u32> {
34        self.inner.peer_pid()
35    }
36    fn pinproj(&mut self) -> Pin<&mut OwnedWriteHalfImpl> {
37        Pin::new(&mut self.inner)
38    }
39}
40
41impl AsyncWrite for OwnedWriteHalf {
42    fn poll_write(
43        mut self: Pin<&mut Self>,
44        cx: &mut Context<'_>,
45        buf: &[u8],
46    ) -> Poll<io::Result<usize>> {
47        self.pinproj().poll_write(cx, buf)
48    }
49    fn poll_write_vectored(
50        mut self: Pin<&mut Self>,
51        cx: &mut Context<'_>,
52        bufs: &[IoSlice<'_>],
53    ) -> Poll<io::Result<usize>> {
54        self.pinproj().poll_write_vectored(cx, bufs)
55    }
56    // Those don't do anything
57    fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
58        self.pinproj().poll_flush(cx)
59    }
60    fn poll_close(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
61        self.pinproj().poll_close(cx)
62    }
63}
64
65impl Debug for OwnedWriteHalf {
66    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
67        Debug::fmt(&self.inner, f)
68    }
69}
70
71// TODO can't do this on Unix
72//impl_as_raw_handle!(OwnedWriteHalf);