-
Notifications
You must be signed in to change notification settings - Fork 75
feat: Expose other kqueue filters #112
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
397009e
feat: Expose other kqueue filters
notgull 069126d
Fix windows build error
notgull 0a367ca
rustfmt
notgull 0962527
Split registration.rs into three files
notgull 7133b0c
Adjust debug output
notgull c080b95
Replace the Async extension with Filter
notgull 318dd73
Review comments
notgull 8209337
Inline small functions
notgull File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
//! Uses the `async_io::os::kqueue` module to wait for a process to terminate. | ||
//! | ||
//! Run with: | ||
//! | ||
//! ``` | ||
//! cargo run --example kqueue-process | ||
//! ``` | ||
|
||
#[cfg(any( | ||
target_os = "macos", | ||
target_os = "ios", | ||
target_os = "tvos", | ||
target_os = "watchos", | ||
target_os = "freebsd", | ||
target_os = "netbsd", | ||
target_os = "openbsd", | ||
target_os = "dragonfly", | ||
))] | ||
fn main() -> std::io::Result<()> { | ||
use std::process::Command; | ||
|
||
use async_io::os::kqueue::{AsyncKqueueExt, Exit}; | ||
use async_io::Async; | ||
use futures_lite::future; | ||
|
||
future::block_on(async { | ||
// Spawn a process. | ||
let process = Command::new("sleep") | ||
.arg("3") | ||
.spawn() | ||
.expect("failed to spawn process"); | ||
|
||
// Wrap the process in an `Async` object that waits for it to exit. | ||
let process = Async::with_filter(Exit::new(process))?; | ||
|
||
// Wait for the process to exit. | ||
process.readable().await?; | ||
|
||
Ok(()) | ||
}) | ||
} | ||
|
||
#[cfg(not(any( | ||
target_os = "macos", | ||
target_os = "ios", | ||
target_os = "tvos", | ||
target_os = "watchos", | ||
target_os = "freebsd", | ||
target_os = "netbsd", | ||
target_os = "openbsd", | ||
target_os = "dragonfly", | ||
)))] | ||
fn main() { | ||
println!("This example only works for kqueue-enabled platforms."); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
//! Platform-specific functionality. | ||
|
||
#[cfg(any( | ||
target_os = "macos", | ||
target_os = "ios", | ||
target_os = "tvos", | ||
target_os = "watchos", | ||
target_os = "freebsd", | ||
target_os = "netbsd", | ||
target_os = "openbsd", | ||
target_os = "dragonfly", | ||
))] | ||
pub mod kqueue; | ||
|
||
mod __private { | ||
#[doc(hidden)] | ||
pub trait AsyncSealed {} | ||
|
||
impl<T> AsyncSealed for crate::Async<T> {} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
//! Functionality that is only available for `kqueue`-based platforms. | ||
|
||
use super::__private::AsyncSealed; | ||
use __private::FilterSealed; | ||
|
||
use crate::reactor::{Reactor, Registration}; | ||
use crate::Async; | ||
|
||
use std::io::Result; | ||
use std::process::Child; | ||
|
||
/// An extension trait for [`Async`](crate::Async) that provides the ability to register other | ||
/// queueable objects into the reactor. | ||
/// | ||
/// The underlying `kqueue` implementation can be used to poll for events besides file descriptor | ||
/// read/write readiness. This API makes these faculties available to the user. | ||
/// | ||
/// See the [`Filter`] trait and its implementors for objects that currently support being registered | ||
/// into the reactor. | ||
pub trait AsyncKqueueExt<T: Filter>: AsyncSealed { | ||
/// Create a new [`Async`](crate::Async) around a [`Filter`]. | ||
/// | ||
/// # Examples | ||
/// | ||
/// ```no_run | ||
/// use std::process::Command; | ||
/// | ||
/// use async_io::Async; | ||
/// use async_io::os::kqueue::{AsyncKqueueExt, Exit}; | ||
/// | ||
/// // Create a new process to wait for. | ||
/// let mut child = Command::new("sleep").arg("5").spawn().unwrap(); | ||
/// | ||
/// // Wrap the process in an `Async` object that waits for it to exit. | ||
/// let process = Async::with_filter(Exit::new(child)).unwrap(); | ||
/// | ||
/// // Wait for the process to exit. | ||
/// # async_io::block_on(async { | ||
/// process.readable().await.unwrap(); | ||
/// # }); | ||
/// ``` | ||
fn with_filter(filter: T) -> Result<Async<T>>; | ||
} | ||
|
||
impl<T: Filter> AsyncKqueueExt<T> for Async<T> { | ||
fn with_filter(mut filter: T) -> Result<Async<T>> { | ||
Ok(Async { | ||
source: Reactor::get().insert_io(filter.registration())?, | ||
io: Some(filter), | ||
}) | ||
} | ||
} | ||
|
||
/// Objects that can be registered into the reactor via a [`Async`](crate::Async). | ||
pub trait Filter: FilterSealed {} | ||
|
||
/// An object representing a signal. | ||
/// | ||
/// When registered into [`Async`](crate::Async) via [`with_filter`](AsyncKqueueExt::with_filter), | ||
/// it will return a [`readable`](crate::Async::readable) event when the signal is received. | ||
#[derive(Debug, Copy, Clone, Eq, PartialEq, PartialOrd, Ord, Hash)] | ||
pub struct Signal(pub i32); | ||
|
||
impl FilterSealed for Signal { | ||
fn registration(&mut self) -> Registration { | ||
(*self).into() | ||
} | ||
} | ||
impl Filter for Signal {} | ||
|
||
/// Wait for a child process to exit. | ||
/// | ||
/// When registered into [`Async`](crate::Async) via [`with_filter`](AsyncKqueueExt::with_filter), | ||
/// it will return a [`readable`](crate::Async::readable) event when the child process exits. | ||
#[derive(Debug)] | ||
pub struct Exit(Option<Child>); | ||
|
||
impl Exit { | ||
/// Create a new `Exit` object. | ||
pub fn new(child: Child) -> Self { | ||
Self(Some(child)) | ||
} | ||
} | ||
|
||
impl FilterSealed for Exit { | ||
fn registration(&mut self) -> Registration { | ||
self.0.take().expect("Cannot reregister child").into() | ||
} | ||
} | ||
impl Filter for Exit {} | ||
|
||
mod __private { | ||
use crate::reactor::Registration; | ||
|
||
#[doc(hidden)] | ||
pub trait FilterSealed { | ||
/// Get a registration object for this filter. | ||
fn registration(&mut self) -> Registration; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.