From 7707ba88efa9b9d78436f1fbdc873d81bfc3f7a5 Mon Sep 17 00:00:00 2001 From: HK416-is-all-you-need <73653352+HK416-is-all-you-need@users.noreply.github.com> Date: Mon, 30 Nov 2020 22:11:18 +0300 Subject: io: add AsyncFd::with_interest (#3167) Fixes #3072 --- tokio/src/io/async_fd.rs | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/tokio/src/io/async_fd.rs b/tokio/src/io/async_fd.rs index 99f23fd6..dcc2ad98 100644 --- a/tokio/src/io/async_fd.rs +++ b/tokio/src/io/async_fd.rs @@ -76,6 +76,7 @@ pub struct AsyncFdReadyGuard<'a, T: AsRawFd> { const ALL_INTEREST: Interest = Interest::READABLE.add(Interest::WRITABLE); impl AsyncFd { + #[inline] /// Creates an AsyncFd backed by (and taking ownership of) an object /// implementing [`AsRawFd`]. The backing file descriptor is cached at the /// time of creation. @@ -85,14 +86,28 @@ impl AsyncFd { where T: AsRawFd, { - Self::new_with_handle(inner, Handle::current()) + Self::with_interest(inner, ALL_INTEREST) + } + + #[inline] + /// Creates new instance as `new` with additional ability to customize interest, + /// allowing to specify whether file descriptor will be polled for read, write or both. + pub fn with_interest(inner: T, interest: Interest) -> io::Result + where + T: AsRawFd, + { + Self::new_with_handle_and_interest(inner, Handle::current(), interest) } - pub(crate) fn new_with_handle(inner: T, handle: Handle) -> io::Result { + pub(crate) fn new_with_handle_and_interest( + inner: T, + handle: Handle, + interest: Interest, + ) -> io::Result { let fd = inner.as_raw_fd(); let registration = - Registration::new_with_interest_and_handle(&mut SourceFd(&fd), ALL_INTEREST, handle)?; + Registration::new_with_interest_and_handle(&mut SourceFd(&fd), interest, handle)?; Ok(AsyncFd { registration, -- cgit v1.2.3