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

Introduce AsyncFdBuilder #3077

Closed
Show file tree
Hide file tree
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
61 changes: 61 additions & 0 deletions tokio/src/io/async_fd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,3 +335,64 @@ impl<T: AsRawFd> AsyncFd<T> {
self.readiness(mio::Interest::WRITABLE).await
}
}

#[derive(Clone, Copy, Debug)]
/// Builder for `AsyncFd` allowing to choose interest on creation.
///
/// By default unless user chooses anything, all interest is assumed.
pub struct AsyncFdBuilder {
is_read: bool,
is_write: bool,
}

impl AsyncFdBuilder {
/// Creates new instance.
pub const fn new() -> Self {
Self {
is_read: false,
is_write: false,
}
}

/// Sets `read` interest
pub const fn read(mut self) -> Self {
self.is_read = true;
self
}

/// Sets `write` interest
pub const fn write(mut self) -> Self {
self.is_write = true;
self
}

#[inline]
/// Constructs new `AsyncFd` instance
pub fn build<T: AsRawFd>(self, fd: T) -> io::Result<AsyncFd<T>> {
Self::build_with_handle(self, fd, Handle::current())
}

fn build_with_handle<T: AsRawFd>(self, inner: T, handle: Handle) -> io::Result<AsyncFd<T>> {
let fd = inner.as_raw_fd();
let interest = match (self.is_read, self.is_write) {
(true, true) | (false, false) => ALL_INTEREST,
(true, false) => mio::Interest::READABLE,
(false, true) => mio::Interest::WRITABLE,
};

let shared = if let Some(inner) = handle.inner() {
inner.add_source(&mut SourceFd(&fd), interest)?
} else {
return Err(io::Error::new(
io::ErrorKind::Other,
"failed to find event loop",
));
};

Ok(AsyncFd {
handle,
shared,
inner: Some(inner),
})
}
}
2 changes: 1 addition & 1 deletion tokio/src/io/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ cfg_net_unix! {

pub mod unix {
//! Asynchronous IO structures specific to Unix-like operating systems.
pub use super::async_fd::{AsyncFd, AsyncFdReadyGuard};
pub use super::async_fd::{AsyncFd, AsyncFdBuilder, AsyncFdReadyGuard};
}
}

Expand Down
19 changes: 18 additions & 1 deletion tokio/tests/io_async_fd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use nix::unistd::{close, read, write};

use futures::{poll, FutureExt};

use tokio::io::unix::{AsyncFd, AsyncFdReadyGuard};
use tokio::io::unix::{AsyncFd, AsyncFdBuilder, AsyncFdReadyGuard};
use tokio_test::{assert_err, assert_pending};

struct TestWaker {
Expand Down Expand Up @@ -183,6 +183,23 @@ async fn initially_writable() {
}
}

#[tokio::test]
async fn initially_not_writable() {
let (a, b) = socketpair();

let afd_a = AsyncFdBuilder::new().read().build(a).unwrap();
let afd_b = AsyncFdBuilder::new().read().build(b).unwrap();

afd_a.writable().await.unwrap_err();
afd_b.writable().await.unwrap_err();

futures::select_biased! {
_ = tokio::time::sleep(Duration::from_millis(10)).fuse() => {},
_ = afd_a.readable().fuse() => panic!("Unexpected readable state"),
_ = afd_b.readable().fuse() => panic!("Unexpected readable state"),
}
}

#[tokio::test]
async fn reset_readable() {
let (a, mut b) = socketpair();
Expand Down