1 use std
::convert
::TryFrom
;
3 use std
::marker
::PhantomData
;
4 use std
::os
::unix
::io
::{AsRawFd, FromRawFd, IntoRawFd, RawFd}
;
6 use std
::task
::{Context, Poll}
;
8 use tokio
::io
::{AsyncRead, AsyncWrite}
;
10 use crate::error
::io_err_other
;
11 use crate::io
::polled_fd
::PolledFd
;
12 use crate::io
::rw_traits
;
15 pub use rw_traits
::{Read, Write}
;
19 _phantom
: PhantomData
<RW
>,
22 impl<RW
> AsRawFd
for Pipe
<RW
> {
24 fn as_raw_fd(&self) -> RawFd
{
29 impl<RW
> IntoRawFd
for Pipe
<RW
> {
31 fn into_raw_fd(self) -> RawFd
{
36 pub fn pipe() -> io
::Result
<(Pipe
<rw_traits
::Read
>, Pipe
<rw_traits
::Write
>)> {
37 let mut pfd
: [RawFd
; 2] = [0, 0];
39 c_try
!(unsafe { libc::pipe2(pfd.as_mut_ptr(), libc::O_CLOEXEC) }
);
41 let (fd_in
, fd_out
) = unsafe { (Fd::from_raw_fd(pfd[0]), Fd::from_raw_fd(pfd[1])) }
;
45 fd
: PolledFd
::new(fd_in
)?
,
46 _phantom
: PhantomData
,
49 fd
: PolledFd
::new(fd_out
)?
,
50 _phantom
: PhantomData
,
55 impl<RW
: rw_traits
::HasRead
> AsyncRead
for Pipe
<RW
> {
60 ) -> Poll
<io
::Result
<usize>> {
61 self.fd
.wrap_read(cx
, || {
62 let fd
= self.as_raw_fd();
63 let size
= libc
::size_t
::try_from(buf
.len()).map_err(io_err_other
)?
;
64 c_result
!(unsafe { libc::read(fd, buf.as_mut_ptr() as *mut libc::c_void, size) }
)
65 .map(|res
| res
as usize)
70 impl<RW
: rw_traits
::HasWrite
> AsyncWrite
for Pipe
<RW
> {
75 ) -> Poll
<io
::Result
<usize>> {
76 self.fd
.wrap_write(cx
, || {
77 let fd
= self.as_raw_fd();
78 let size
= libc
::size_t
::try_from(buf
.len()).map_err(io_err_other
)?
;
79 c_result
!(unsafe { libc::write(fd, buf.as_ptr() as *const libc::c_void, size) }
)
80 .map(|res
| res
as usize)
84 fn poll_flush(self: Pin
<&mut Self>, _cx
: &mut Context
<'_
>) -> Poll
<io
::Result
<()>> {
88 fn poll_shutdown(self: Pin
<&mut Self>, _cx
: &mut Context
<'_
>) -> Poll
<io
::Result
<()>> {