interprocess_docfix/os/unix/udsocket/tokio/stream/
read_half.rs

1#[cfg(uds_supported)]
2use super::c_wrappers;
3use super::{OwnedWriteHalf, ReuniteError, UdStream};
4use crate::os::unix::imports::*;
5use std::{
6    io,
7    net::Shutdown,
8    pin::Pin,
9    task::{Context, Poll},
10};
11
12/// Borrowed read half of a [`UdStream`](super::UdStream), created by [`.split()`](super::UdStream::split).
13#[derive(Debug)]
14pub struct BorrowedReadHalf<'a>(pub(super) TokioUdStreamReadHalf<'a>);
15
16impl<'a> BorrowedReadHalf<'a> {
17    /// Fetches the credentials of the other end of the connection without using ancillary data. The returned structure contains the process identifier, user identifier and group identifier of the peer.
18    #[cfg(any(doc, uds_peercred))]
19    #[cfg_attr( // uds_peercred template
20        feature = "doc_cfg",
21        doc(cfg(any(
22            all(
23                target_os = "linux",
24                any(
25                    target_env = "gnu",
26                    target_env = "musl",
27                    target_env = "musleabi",
28                    target_env = "musleabihf"
29                )
30            ),
31            target_os = "emscripten",
32            target_os = "redox",
33            target_os = "haiku"
34        )))
35    )]
36    pub fn get_peer_credentials(&self) -> io::Result<ucred> {
37        c_wrappers::get_peer_ucred(self.as_stream_raw_fd().as_ref())
38    }
39    /// Shuts down the read half.
40    ///
41    /// Attempting to call this method multiple times may return `Ok(())` every time or it may return an error the second time it is called, depending on the platform. You must either avoid using the same value twice or ignore the error entirely.
42    pub fn shutdown(&self) -> io::Result<()> {
43        c_wrappers::shutdown(self.as_stream_raw_fd().as_ref(), Shutdown::Read)
44    }
45
46    /// Returns the underlying file descriptor. Note that this isn't a file descriptor for the read half specifically, but rather for the whole stream, so this isn't exposed as a struct method.
47    fn as_stream_raw_fd(&self) -> c_int {
48        let stream: &TokioUdStream = self.0.as_ref();
49        stream.as_raw_fd()
50    }
51
52    fn pinproject(self: Pin<&mut Self>) -> Pin<&mut TokioUdStreamReadHalf<'a>> {
53        Pin::new(&mut self.get_mut().0)
54    }
55
56    tokio_wrapper_conversion_methods!(tokio_norawfd TokioUdStreamReadHalf<'a>);
57}
58
59#[cfg(feature = "tokio_support")]
60impl TokioAsyncRead for BorrowedReadHalf<'_> {
61    fn poll_read(
62        self: Pin<&mut Self>,
63        cx: &mut Context<'_>,
64        buf: &mut ReadBuf<'_>,
65    ) -> Poll<io::Result<()>> {
66        self.pinproject().poll_read(cx, buf)
67    }
68}
69#[cfg(feature = "tokio_support")]
70impl FuturesAsyncRead for BorrowedReadHalf<'_> {
71    fn poll_read(
72        self: Pin<&mut Self>,
73        cx: &mut Context<'_>,
74        buf: &mut [u8],
75    ) -> Poll<io::Result<usize>> {
76        let mut buf = ReadBuf::new(buf);
77        match self.pinproject().poll_read(cx, &mut buf) {
78            Poll::Ready(Ok(())) => Poll::Ready(Ok(buf.filled().len())),
79            Poll::Ready(Err(e)) => Poll::Ready(Err(e)),
80            Poll::Pending => Poll::Pending,
81        }
82    }
83}
84
85tokio_wrapper_trait_impls!(
86    for BorrowedReadHalf<'a>, tokio_norawfd_lt 'a TokioUdStreamReadHalf<'a>);
87
88/// Owned read half of a [`UdStream`](super::UdStream), created by [`.into_split()`](super::UdStream::into_split).
89#[derive(Debug)]
90pub struct OwnedReadHalf(pub(super) TokioUdStreamOwnedReadHalf);
91impl OwnedReadHalf {
92    /// Attempts to put two owned halves of a stream back together and recover the original stream. Succeeds only if the two halves originated from the same call to [`.into_split()`](UdStream::into_split).
93    pub fn reunite_with(self, write: OwnedWriteHalf) -> Result<UdStream, ReuniteError> {
94        UdStream::reunite(self, write)
95    }
96
97    /// Fetches the credentials of the other end of the connection without using ancillary data. The returned structure contains the process identifier, user identifier and group identifier of the peer.
98    #[cfg(any(doc, uds_peercred))]
99    #[cfg_attr( // uds_peercred template
100        feature = "doc_cfg",
101        doc(cfg(any(
102            all(
103                target_os = "linux",
104                any(
105                    target_env = "gnu",
106                    target_env = "musl",
107                    target_env = "musleabi",
108                    target_env = "musleabihf"
109                )
110            ),
111            target_os = "emscripten",
112            target_os = "redox",
113            target_os = "haiku"
114        )))
115    )]
116    pub fn get_peer_credentials(&self) -> io::Result<ucred> {
117        c_wrappers::get_peer_ucred(self.as_stream_raw_fd().as_ref())
118    }
119
120    /// Shuts down the read half.
121    ///
122    /// Attempting to call this method multiple times may return `Ok(())` every time or it may return an error the second time it is called, depending on the platform. You must either avoid using the same value twice or ignore the error entirely.
123    pub fn shutdown(&self) -> io::Result<()> {
124        c_wrappers::shutdown(self.as_stream_raw_fd().as_ref(), Shutdown::Read)
125    }
126
127    /// Returns the underlying file descriptor. Note that this isn't a file descriptor for the read half specifically, but rather for the whole stream, so this isn't exposed as a struct method.
128    fn as_stream_raw_fd(&self) -> c_int {
129        let stream: &TokioUdStream = self.0.as_ref();
130        stream.as_raw_fd()
131    }
132
133    fn pinproject(self: Pin<&mut Self>) -> Pin<&mut TokioUdStreamOwnedReadHalf> {
134        Pin::new(&mut self.get_mut().0)
135    }
136
137    tokio_wrapper_conversion_methods!(tokio_norawfd TokioUdStreamOwnedReadHalf);
138}
139
140#[cfg(feature = "tokio_support")]
141impl TokioAsyncRead for OwnedReadHalf {
142    fn poll_read(
143        self: Pin<&mut Self>,
144        cx: &mut Context<'_>,
145        buf: &mut ReadBuf<'_>,
146    ) -> Poll<io::Result<()>> {
147        self.pinproject().poll_read(cx, buf)
148    }
149}
150#[cfg(feature = "tokio_support")]
151impl FuturesAsyncRead for OwnedReadHalf {
152    fn poll_read(
153        self: Pin<&mut Self>,
154        cx: &mut Context<'_>,
155        buf: &mut [u8],
156    ) -> Poll<io::Result<usize>> {
157        let mut buf = ReadBuf::new(buf);
158        match self.pinproject().poll_read(cx, &mut buf) {
159            Poll::Ready(Ok(())) => Poll::Ready(Ok(buf.filled().len())),
160            Poll::Ready(Err(e)) => Poll::Ready(Err(e)),
161            Poll::Pending => Poll::Pending,
162        }
163    }
164}
165
166tokio_wrapper_trait_impls!(
167    for OwnedReadHalf, tokio_norawfd TokioUdStreamOwnedReadHalf);