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 add_outlives method to Poller #171

Closed
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
35 changes: 27 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,13 +349,15 @@ impl Event {
}

/// Waits for I/O events.
pub struct Poller {
pub struct Poller<'a> {
poller: sys::Poller,
lock: Mutex<()>,
notified: AtomicBool,
// Being contravariant over 'a requires borrowed file descriptors outlive the `Poller`
_marker: PhantomData<fn(&'a ())>,
}

impl Poller {
impl<'a> Poller<'a> {
/// Creates a new poller.
///
/// # Examples
Expand All @@ -366,11 +368,12 @@ impl Poller {
/// let poller = Poller::new()?;
/// # std::io::Result::Ok(())
/// ```
pub fn new() -> io::Result<Poller> {
pub fn new() -> io::Result<Poller<'a>> {
Ok(Poller {
poller: sys::Poller::new()?,
lock: Mutex::new(()),
notified: AtomicBool::new(false),
_marker: PhantomData,
})
}

Expand Down Expand Up @@ -478,6 +481,22 @@ impl Poller {
self.poller.add(source.raw(), interest, mode)
}

/// Adds a file descriptor or socket to the poller safely.
///
/// This is identical to the `add()` function, but ensures safety
/// by requiring the descriptor to always outlive the `Poller`.
///
/// # Errors
///
/// If the operating system does not support the specified mode, this function
/// will return an error.
pub fn add_outlives<T: AsSource>(&self, source: &'a T, interest: Event) -> io::Result<()> {
// SAFETY: Lifetime parameter ensures AsSource will live at least as long as the Poller
// It is unsound for AsSource to reference descriptors it doesn't own,
// so assume reference from `source.as_fd()` will be valid until self is dropped
unsafe { self.add(source.as_fd().as_raw_fd(), interest) }
}

/// Modifies the interest in a file descriptor or socket.
///
/// This method has the same behavior as [`add()`][`Poller::add()`] except it modifies the
Expand Down Expand Up @@ -915,13 +934,13 @@ mod raw_fd_impl {
use crate::Poller;
use std::os::unix::io::{AsFd, AsRawFd, BorrowedFd, RawFd};

impl AsRawFd for Poller {
impl AsRawFd for Poller<'_> {
fn as_raw_fd(&self) -> RawFd {
self.poller.as_raw_fd()
}
}

impl AsFd for Poller {
impl AsFd for Poller<'_> {
fn as_fd(&self) -> BorrowedFd<'_> {
self.poller.as_fd()
}
Expand All @@ -947,7 +966,7 @@ mod raw_handle_impl {
}
}

impl fmt::Debug for Poller {
impl fmt::Debug for Poller<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.poller.fmt(f)
}
Expand Down Expand Up @@ -1026,8 +1045,8 @@ fn _assert_send_and_sync() {
fn assert_send<T: Send>() {}
fn assert_sync<T: Sync>() {}

assert_send::<Poller>();
assert_sync::<Poller>();
assert_send::<Poller<'_>>();
assert_sync::<Poller<'_>>();

assert_send::<Event>();
assert_sync::<Event>();
Expand Down
2 changes: 1 addition & 1 deletion src/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ mod __private {
#[doc(hidden)]
pub trait PollerSealed {}

impl PollerSealed for crate::Poller {}
impl PollerSealed for crate::Poller<'_> {}
}
Loading