interprocess/local_socket/stream/
enum.rs1#[cfg(unix)]
2use crate::os::unix::uds_local_socket as uds_impl;
3#[cfg(windows)]
4use crate::os::windows::named_pipe::local_socket as np_impl;
5use {
6 super::r#trait,
7 crate::{local_socket::Name, TryClone},
8 std::io::{self, prelude::*, IoSlice, IoSliceMut},
9};
10
11impmod! {local_socket::dispatch_sync}
12
13macro_rules! dispatch_read {
14 (@iw $ty:ident) => {
15 #[inline]
16 fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
17 dispatch!($ty: x in self => x.read(buf))
18 }
19 #[inline]
20 fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
21 dispatch!($ty: x in self => x.read_vectored(bufs))
22 }
23 };
24 ($ty:ident) => {
25 impl Read for &$ty {
26 dispatch_read!(@iw $ty);
27 }
28 impl Read for $ty {
29 dispatch_read!(@iw $ty);
30 }
31 };
32}
33macro_rules! dispatch_write {
34 (@iw $ty:ident) => {
35 #[inline]
36 fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
37 dispatch!($ty: x in self => x.write(buf))
38 }
39 #[inline]
40 fn flush(&mut self) -> io::Result<()> {
41 Ok(())
42 }
43 #[inline]
44 fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
45 dispatch!($ty: x in self => x.write_vectored(bufs))
46 }
47 };
48 ($ty:ident) => {
49 impl Write for &$ty {
51 dispatch_write!(@iw $ty);
52 }
53 impl Write for $ty {
55 dispatch_write!(@iw $ty);
56 }
57 };
58}
59
60mkenum!(
61#[doc = doctest_file::include_doctest!("examples/local_socket/sync/stream.rs")]
69Stream);
71impl r#trait::Stream for Stream {
72 type RecvHalf = RecvHalf;
73 type SendHalf = SendHalf;
74
75 #[inline]
76 fn connect(name: Name<'_>) -> io::Result<Self> { dispatch_sync::connect(name) }
77 #[inline]
78 fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
79 dispatch!(Self: x in self => x.set_nonblocking(nonblocking))
80 }
81 fn split(self) -> (RecvHalf, SendHalf) {
82 match self {
83 #[cfg(windows)]
84 Stream::NamedPipe(s) => {
85 let (rh, sh) = s.split();
86 (RecvHalf::NamedPipe(rh), SendHalf::NamedPipe(sh))
87 }
88 #[cfg(unix)]
89 Stream::UdSocket(s) => {
90 let (rh, sh) = s.split();
91 (RecvHalf::UdSocket(rh), SendHalf::UdSocket(sh))
92 }
93 }
94 }
95 fn reunite(rh: RecvHalf, sh: SendHalf) -> ReuniteResult {
96 match (rh, sh) {
97 #[cfg(windows)]
98 (RecvHalf::NamedPipe(rh), SendHalf::NamedPipe(sh)) => {
99 np_impl::Stream::reunite(rh, sh).map(From::from).map_err(|e| e.convert_halves())
100 }
101 #[cfg(unix)]
102 (RecvHalf::UdSocket(rh), SendHalf::UdSocket(sh)) => {
103 uds_impl::Stream::reunite(rh, sh).map(From::from).map_err(|e| e.convert_halves())
104 }
105 #[allow(unreachable_patterns)]
106 (rh, sh) => Err(ReuniteError { rh, sh }),
107 }
108 }
109}
110impl TryClone for Stream {
111 fn try_clone(&self) -> io::Result<Self> {
112 dispatch!(Self: x in self => x.try_clone()).map(From::from)
113 }
114}
115multimacro! {
116 Stream,
117 dispatch_read,
118 dispatch_write,
119}
120
121mkenum!(
122"local_socket::" RecvHalf);
124impl r#trait::RecvHalf for RecvHalf {
125 type Stream = Stream;
126}
127dispatch_read!(RecvHalf);
128
129mkenum!(
130"local_socket::" SendHalf);
132impl r#trait::SendHalf for SendHalf {
133 type Stream = Stream;
134}
135dispatch_write!(SendHalf);
136
137pub type ReuniteError = crate::error::ReuniteError<RecvHalf, SendHalf>;
139
140pub type ReuniteResult = r#trait::ReuniteResult<Stream>;