Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add AsyncFd::with_interest #3167

Merged
merged 3 commits into from
Nov 30, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions tokio/src/io/async_fd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ pub struct AsyncFdReadyGuard<'a, T: AsRawFd> {
const ALL_INTEREST: Interest = Interest::READABLE.add(Interest::WRITABLE);

impl<T: AsRawFd> AsyncFd<T> {
#[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.
Expand All @@ -85,14 +86,28 @@ impl<T: AsRawFd> AsyncFd<T> {
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<Self>
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<Self> {
pub(crate) fn new_with_handle_and_interest(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We may also want to rename this to with_handle_and_interest for consistency, but definitely not a blocker. I will merge this shortly if nobody has any other comments.

inner: T,
handle: Handle,
interest: Interest,
) -> io::Result<Self> {
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,
Expand Down