Skip to content

Commit 594abec

Browse files
committed
Refactor: Put mod unix & windows into separate files
Signed-off-by: Jiahao XU <[email protected]>
1 parent 4c6b6bb commit 594abec

File tree

3 files changed

+202
-205
lines changed

3 files changed

+202
-205
lines changed

library/std/src/sys/anonymous_pipe/mod.rs

Lines changed: 2 additions & 205 deletions
Original file line numberDiff line numberDiff line change
@@ -88,213 +88,10 @@ forward_io_write_traits!(PipeWriter);
8888
forward_io_write_traits!(&PipeWriter);
8989

9090
#[cfg(unix)]
91-
mod unix {
92-
use super::*;
93-
94-
use crate::{
95-
fs::File,
96-
os::{
97-
fd::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, OwnedFd, RawFd},
98-
unix::fs::FileTypeExt,
99-
},
100-
sys::{
101-
fd::FileDesc,
102-
pipe::{anon_pipe, AnonPipe},
103-
},
104-
sys_common::{FromInner, IntoInner},
105-
};
106-
107-
#[inline]
108-
pub(super) fn pipe() -> io::Result<(PipeReader, PipeWriter)> {
109-
anon_pipe().map(|(rx, tx)| (PipeReader(rx), PipeWriter(tx)))
110-
}
111-
112-
macro_rules! impl_traits {
113-
($name:ty) => {
114-
#[unstable(feature = "anonymous_pipe", issue = "127154")]
115-
impl AsFd for $name {
116-
fn as_fd(&self) -> BorrowedFd<'_> {
117-
self.0.as_fd()
118-
}
119-
}
120-
#[unstable(feature = "anonymous_pipe", issue = "127154")]
121-
impl AsRawFd for $name {
122-
fn as_raw_fd(&self) -> RawFd {
123-
self.0.as_raw_fd()
124-
}
125-
}
126-
#[unstable(feature = "anonymous_pipe", issue = "127154")]
127-
impl From<$name> for OwnedFd {
128-
fn from(pipe: $name) -> Self {
129-
FileDesc::into_inner(AnonPipe::into_inner(pipe.0))
130-
}
131-
}
132-
#[unstable(feature = "anonymous_pipe", issue = "127154")]
133-
impl FromRawFd for $name {
134-
unsafe fn from_raw_fd(raw_fd: RawFd) -> Self {
135-
Self(AnonPipe::from_raw_fd(raw_fd))
136-
}
137-
}
138-
#[unstable(feature = "anonymous_pipe", issue = "127154")]
139-
impl IntoRawFd for $name {
140-
fn into_raw_fd(self) -> RawFd {
141-
self.0.into_raw_fd()
142-
}
143-
}
144-
#[unstable(feature = "anonymous_pipe", issue = "127154")]
145-
impl From<$name> for Stdio {
146-
fn from(pipe: $name) -> Self {
147-
Self::from(OwnedFd::from(pipe))
148-
}
149-
}
150-
};
151-
}
152-
impl_traits!(PipeReader);
153-
impl_traits!(PipeWriter);
154-
155-
fn convert_to_pipe(owned_fd: OwnedFd) -> io::Result<AnonPipe> {
156-
let file = File::from(owned_fd);
157-
if file.metadata()?.file_type().is_fifo() {
158-
Ok(AnonPipe::from_inner(FileDesc::from_inner(OwnedFd::from(file))))
159-
} else {
160-
Err(io::Error::new(io::ErrorKind::InvalidInput, "Not a pipe"))
161-
}
162-
}
163-
164-
#[unstable(feature = "anonymous_pipe", issue = "127154")]
165-
impl TryFrom<OwnedFd> for PipeReader {
166-
type Error = io::Error;
167-
168-
fn try_from(owned_fd: OwnedFd) -> Result<Self, Self::Error> {
169-
convert_to_pipe(owned_fd)
170-
.and_then(|pipe| {
171-
if pipe.as_file_desc().get_access_mode()?.readable {
172-
Ok(pipe)
173-
} else {
174-
Err(io::Error::new(
175-
io::ErrorKind::InvalidInput,
176-
format!("Pipe {} is not readable", pipe.as_raw_fd()),
177-
))
178-
}
179-
})
180-
.map(Self)
181-
}
182-
}
183-
184-
#[unstable(feature = "anonymous_pipe", issue = "127154")]
185-
impl TryFrom<OwnedFd> for PipeWriter {
186-
type Error = io::Error;
187-
188-
fn try_from(owned_fd: OwnedFd) -> Result<Self, Self::Error> {
189-
convert_to_pipe(owned_fd)
190-
.and_then(|pipe| {
191-
if pipe.as_file_desc().get_access_mode()?.writable {
192-
Ok(pipe)
193-
} else {
194-
Err(io::Error::new(
195-
io::ErrorKind::InvalidInput,
196-
format!("Pipe {} is not writable", pipe.as_raw_fd()),
197-
))
198-
}
199-
})
200-
.map(Self)
201-
}
202-
}
203-
}
91+
mod unix;
20492

20593
#[cfg(windows)]
206-
mod windows {
207-
use super::*;
208-
209-
use crate::{
210-
os::windows::io::{
211-
AsHandle, AsRawHandle, BorrowedHandle, FromRawHandle, IntoRawHandle, OwnedHandle,
212-
RawHandle,
213-
},
214-
sys::{
215-
c::{GetFileType, FILE_TYPE_PIPE},
216-
handle::Handle,
217-
pipe::{anon_pipe, AnonPipe, Pipes},
218-
},
219-
sys_common::{FromInner, IntoInner},
220-
};
221-
222-
#[inline]
223-
pub(super) fn pipe() -> io::Result<(PipeReader, PipeWriter)> {
224-
anon_pipe(true, false).map(|Pipes { ours, theirs }| (PipeReader(ours), PipeWriter(theirs)))
225-
}
226-
227-
macro_rules! impl_traits {
228-
($name:ty) => {
229-
#[unstable(feature = "anonymous_pipe", issue = "127154")]
230-
impl AsHandle for $name {
231-
fn as_handle(&self) -> BorrowedHandle<'_> {
232-
self.0.handle().as_handle()
233-
}
234-
}
235-
#[unstable(feature = "anonymous_pipe", issue = "127154")]
236-
impl AsRawHandle for $name {
237-
fn as_raw_handle(&self) -> RawHandle {
238-
self.0.handle().as_raw_handle()
239-
}
240-
}
241-
242-
#[unstable(feature = "anonymous_pipe", issue = "127154")]
243-
impl FromRawHandle for $name {
244-
unsafe fn from_raw_handle(raw_handle: RawHandle) -> Self {
245-
Self(AnonPipe::from_inner(Handle::from_raw_handle(raw_handle)))
246-
}
247-
}
248-
#[unstable(feature = "anonymous_pipe", issue = "127154")]
249-
impl IntoRawHandle for $name {
250-
fn into_raw_handle(self) -> RawHandle {
251-
self.0.into_handle().into_raw_handle()
252-
}
253-
}
254-
255-
#[unstable(feature = "anonymous_pipe", issue = "127154")]
256-
impl From<$name> for OwnedHandle {
257-
fn from(pipe: $name) -> Self {
258-
Handle::into_inner(AnonPipe::into_inner(pipe.0))
259-
}
260-
}
261-
#[unstable(feature = "anonymous_pipe", issue = "127154")]
262-
impl From<$name> for Stdio {
263-
fn from(pipe: $name) -> Self {
264-
Self::from(OwnedHandle::from(pipe))
265-
}
266-
}
267-
};
268-
}
269-
impl_traits!(PipeReader);
270-
impl_traits!(PipeWriter);
271-
272-
fn convert_to_pipe(owned_handle: OwnedHandle) -> io::Result<AnonPipe> {
273-
if unsafe { GetFileType(owned_handle.as_raw_handle()) } == FILE_TYPE_PIPE {
274-
Ok(AnonPipe::from_inner(Handle::from_inner(owned_handle)))
275-
} else {
276-
Err(io::Error::new(io::ErrorKind::InvalidInput, "Not a pipe"))
277-
}
278-
}
279-
280-
#[unstable(feature = "anonymous_pipe", issue = "127154")]
281-
impl TryFrom<OwnedHandle> for PipeReader {
282-
type Error = io::Error;
283-
284-
fn try_from(owned_handle: OwnedHandle) -> Result<Self, Self::Error> {
285-
convert_to_pipe(owned_handle).map(Self)
286-
}
287-
}
288-
289-
#[unstable(feature = "anonymous_pipe", issue = "127154")]
290-
impl TryFrom<OwnedHandle> for PipeWriter {
291-
type Error = io::Error;
292-
293-
fn try_from(owned_handle: OwnedHandle) -> Result<Self, Self::Error> {
294-
convert_to_pipe(owned_handle).map(Self)
295-
}
296-
}
297-
}
94+
mod windows;
29895

29996
#[cfg(test)]
30097
mod tests;
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
use super::*;
2+
3+
use crate::{
4+
fs::File,
5+
os::{
6+
fd::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, OwnedFd, RawFd},
7+
unix::fs::FileTypeExt,
8+
},
9+
sys::{
10+
fd::FileDesc,
11+
pipe::{anon_pipe, AnonPipe},
12+
},
13+
sys_common::{FromInner, IntoInner},
14+
};
15+
16+
#[inline]
17+
pub(super) fn pipe() -> io::Result<(PipeReader, PipeWriter)> {
18+
anon_pipe().map(|(rx, tx)| (PipeReader(rx), PipeWriter(tx)))
19+
}
20+
21+
macro_rules! impl_traits {
22+
($name:ty) => {
23+
#[unstable(feature = "anonymous_pipe", issue = "127154")]
24+
impl AsFd for $name {
25+
fn as_fd(&self) -> BorrowedFd<'_> {
26+
self.0.as_fd()
27+
}
28+
}
29+
#[unstable(feature = "anonymous_pipe", issue = "127154")]
30+
impl AsRawFd for $name {
31+
fn as_raw_fd(&self) -> RawFd {
32+
self.0.as_raw_fd()
33+
}
34+
}
35+
#[unstable(feature = "anonymous_pipe", issue = "127154")]
36+
impl From<$name> for OwnedFd {
37+
fn from(pipe: $name) -> Self {
38+
FileDesc::into_inner(AnonPipe::into_inner(pipe.0))
39+
}
40+
}
41+
#[unstable(feature = "anonymous_pipe", issue = "127154")]
42+
impl FromRawFd for $name {
43+
unsafe fn from_raw_fd(raw_fd: RawFd) -> Self {
44+
Self(AnonPipe::from_raw_fd(raw_fd))
45+
}
46+
}
47+
#[unstable(feature = "anonymous_pipe", issue = "127154")]
48+
impl IntoRawFd for $name {
49+
fn into_raw_fd(self) -> RawFd {
50+
self.0.into_raw_fd()
51+
}
52+
}
53+
#[unstable(feature = "anonymous_pipe", issue = "127154")]
54+
impl From<$name> for Stdio {
55+
fn from(pipe: $name) -> Self {
56+
Self::from(OwnedFd::from(pipe))
57+
}
58+
}
59+
};
60+
}
61+
impl_traits!(PipeReader);
62+
impl_traits!(PipeWriter);
63+
64+
fn convert_to_pipe(owned_fd: OwnedFd) -> io::Result<AnonPipe> {
65+
let file = File::from(owned_fd);
66+
if file.metadata()?.file_type().is_fifo() {
67+
Ok(AnonPipe::from_inner(FileDesc::from_inner(OwnedFd::from(file))))
68+
} else {
69+
Err(io::Error::new(io::ErrorKind::InvalidInput, "Not a pipe"))
70+
}
71+
}
72+
73+
#[unstable(feature = "anonymous_pipe", issue = "127154")]
74+
impl TryFrom<OwnedFd> for PipeReader {
75+
type Error = io::Error;
76+
77+
fn try_from(owned_fd: OwnedFd) -> Result<Self, Self::Error> {
78+
convert_to_pipe(owned_fd)
79+
.and_then(|pipe| {
80+
if pipe.as_file_desc().get_access_mode()?.readable {
81+
Ok(pipe)
82+
} else {
83+
Err(io::Error::new(
84+
io::ErrorKind::InvalidInput,
85+
format!("Pipe {} is not readable", pipe.as_raw_fd()),
86+
))
87+
}
88+
})
89+
.map(Self)
90+
}
91+
}
92+
93+
#[unstable(feature = "anonymous_pipe", issue = "127154")]
94+
impl TryFrom<OwnedFd> for PipeWriter {
95+
type Error = io::Error;
96+
97+
fn try_from(owned_fd: OwnedFd) -> Result<Self, Self::Error> {
98+
convert_to_pipe(owned_fd)
99+
.and_then(|pipe| {
100+
if pipe.as_file_desc().get_access_mode()?.writable {
101+
Ok(pipe)
102+
} else {
103+
Err(io::Error::new(
104+
io::ErrorKind::InvalidInput,
105+
format!("Pipe {} is not writable", pipe.as_raw_fd()),
106+
))
107+
}
108+
})
109+
.map(Self)
110+
}
111+
}

0 commit comments

Comments
 (0)