interprocess_docfix/os/unix/udsocket/tokio/stream/
read_half.rs1#[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#[derive(Debug)]
14pub struct BorrowedReadHalf<'a>(pub(super) TokioUdStreamReadHalf<'a>);
15
16impl<'a> BorrowedReadHalf<'a> {
17 #[cfg(any(doc, uds_peercred))]
19 #[cfg_attr( 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 pub fn shutdown(&self) -> io::Result<()> {
43 c_wrappers::shutdown(self.as_stream_raw_fd().as_ref(), Shutdown::Read)
44 }
45
46 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#[derive(Debug)]
90pub struct OwnedReadHalf(pub(super) TokioUdStreamOwnedReadHalf);
91impl OwnedReadHalf {
92 pub fn reunite_with(self, write: OwnedWriteHalf) -> Result<UdStream, ReuniteError> {
94 UdStream::reunite(self, write)
95 }
96
97 #[cfg(any(doc, uds_peercred))]
99 #[cfg_attr( 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 pub fn shutdown(&self) -> io::Result<()> {
124 c_wrappers::shutdown(self.as_stream_raw_fd().as_ref(), Shutdown::Read)
125 }
126
127 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);