2 use std
::os
::unix
::io
::{AsRawFd, FromRawFd, IntoRawFd, RawFd}
;
3 use std
::task
::{Context, Poll}
;
5 use tokio
::io
::unix
::AsyncFd
;
10 pub struct EventedFd
{
16 pub fn new(fd
: Fd
) -> Self {
21 impl AsRawFd
for EventedFd
{
23 fn as_raw_fd(&self) -> RawFd
{
28 impl FromRawFd
for EventedFd
{
30 unsafe fn from_raw_fd(fd
: RawFd
) -> Self {
31 Self::new(Fd
::from_raw_fd(fd
))
35 impl IntoRawFd
for EventedFd
{
37 fn into_raw_fd(self) -> RawFd
{
44 fd
: AsyncFd
<EventedFd
>,
48 pub fn new(fd
: Fd
) -> tokio
::io
::Result
<Self> {
50 fd
: AsyncFd
::new(EventedFd
::new(fd
))?
,
57 func
: impl FnOnce() -> io
::Result
<T
>,
58 ) -> Poll
<io
::Result
<T
>> {
59 let mut ready_guard
= ready
!(self.fd
.poll_read_ready(cx
))?
;
61 Ok(out
) => Poll
::Ready(Ok(out
)),
62 Err(ref err
) if err
.kind() == io
::ErrorKind
::WouldBlock
=> {
63 ready_guard
.clear_ready();
66 Err(err
) => Poll
::Ready(Err(err
)),
73 func
: impl FnOnce() -> io
::Result
<T
>,
74 ) -> Poll
<io
::Result
<T
>> {
75 let mut ready_guard
= ready
!(self.fd
.poll_write_ready(cx
))?
;
77 Ok(out
) => Poll
::Ready(Ok(out
)),
78 Err(ref err
) if err
.kind() == io
::ErrorKind
::WouldBlock
=> {
79 ready_guard
.clear_ready();
82 Err(err
) => Poll
::Ready(Err(err
)),
87 impl AsRawFd
for PolledFd
{
89 fn as_raw_fd(&self) -> RawFd
{
90 self.fd
.get_ref().as_raw_fd()
94 impl IntoRawFd
for PolledFd
{
96 fn into_raw_fd(self) -> RawFd
{
97 // for the kind of resource we're managing it should always be possible to extract it from