Skip to content

Commit

Permalink
Expose raw handles for the Poller (#39)
Browse files Browse the repository at this point in the history
* expose raw handles

* add comment explaining no_*
  • Loading branch information
notgull authored Aug 18, 2022
1 parent 61c2179 commit 323473e
Show file tree
Hide file tree
Showing 7 changed files with 150 additions and 2 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ std = []
cfg-if = "1"
log = "0.4.11"

[build-dependencies]
autocfg = "1"

[target.'cfg(any(unix, target_os = "fuchsia", target_os = "vxworks"))'.dependencies]
libc = "0.2.77"

Expand Down
21 changes: 21 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
fn main() {
let cfg = match autocfg::AutoCfg::new() {
Ok(cfg) => cfg,
Err(e) => {
println!(
"cargo:warning=polling: failed to detect compiler features: {}",
e
);
return;
}
};

// We use "no_*" instead of "has_*" here. For non-Cargo
// build tools that don't run build.rs, the negative
// allows us to treat the current Rust version as the
// latest stable version, for when version information
// isn't available.
if !cfg.probe_rustc_version(1, 63) {
autocfg::emit("polling_no_io_safety");
}
}
19 changes: 18 additions & 1 deletion src/epoll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@
use std::convert::TryInto;
use std::io;
use std::os::unix::io::RawFd;
use std::os::unix::io::{AsRawFd, RawFd};
use std::ptr;
use std::time::Duration;

#[cfg(not(polling_no_io_safety))]
use std::os::unix::io::{AsFd, BorrowedFd};

use crate::Event;

/// Interface to epoll.
Expand Down Expand Up @@ -225,6 +228,20 @@ impl Poller {
}
}

impl AsRawFd for Poller {
fn as_raw_fd(&self) -> RawFd {
self.epoll_fd
}
}

#[cfg(not(polling_no_io_safety))]
impl AsFd for Poller {
fn as_fd(&self) -> BorrowedFd<'_> {
// SAFETY: lifetime is bound by "self"
unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) }
}
}

impl Drop for Poller {
fn drop(&mut self) {
log::trace!(
Expand Down
17 changes: 17 additions & 0 deletions src/kqueue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ use std::os::unix::net::UnixStream;
use std::ptr;
use std::time::Duration;

#[cfg(not(polling_no_io_safety))]
use std::os::unix::io::{AsFd, BorrowedFd};

use crate::Event;

/// Interface to kqueue.
Expand Down Expand Up @@ -177,6 +180,20 @@ impl Poller {
}
}

impl AsRawFd for Poller {
fn as_raw_fd(&self) -> RawFd {
self.kqueue_fd
}
}

#[cfg(not(polling_no_io_safety))]
impl AsFd for Poller {
fn as_fd(&self) -> BorrowedFd<'_> {
// SAFETY: lifetime is bound by "self"
unsafe { BorrowedFd::borrow_raw(self.kqueue_fd) }
}
}

impl Drop for Poller {
fn drop(&mut self) {
log::trace!("drop: kqueue_fd={}", self.kqueue_fd);
Expand Down
56 changes: 56 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ cfg_if! {
} else if #[cfg(any(
target_os = "vxworks",
target_os = "fuchsia",
target_os = "horizon",
unix,
))] {
mod poll;
Expand Down Expand Up @@ -441,6 +442,61 @@ impl Poller {
}
}

#[cfg(any(
target_os = "linux",
target_os = "android",
target_os = "illumos",
target_os = "solaris",
target_os = "macos",
target_os = "ios",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd",
target_os = "dragonfly",
))]
mod raw_fd_impl {
use crate::Poller;
use std::os::unix::io::{AsRawFd, RawFd};

#[cfg(not(polling_no_io_safety))]
use std::os::unix::io::{AsFd, BorrowedFd};

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

#[cfg(not(polling_no_io_safety))]
impl AsFd for Poller {
fn as_fd(&self) -> BorrowedFd<'_> {
self.poller.as_fd()
}
}
}

#[cfg(windows)]
mod raw_handle_impl {
use crate::Poller;
use std::os::windows::io::{AsRawHandle, RawHandle};

#[cfg(not(polling_no_io_safety))]
use std::os::windows::io::{AsHandle, BorrowedHandle};

impl AsRawHandle for Poller {
fn as_raw_handle(&self) -> RawHandle {
self.poller.as_raw_handle()
}
}

#[cfg(not(polling_no_io_safety))]
impl AsHandle for Poller {
fn as_handle(&self) -> BorrowedHandle<'_> {
self.poller.as_handle()
}
}
}

impl fmt::Debug for Poller {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.poller.fmt(f)
Expand Down
17 changes: 17 additions & 0 deletions src/port.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ use std::os::unix::net::UnixStream;
use std::ptr;
use std::time::Duration;

#[cfg(not(polling_no_io_safety))]
use std::os::unix::io::{AsFd, BorrowedFd};

use crate::Event;

/// Interface to event ports.
Expand Down Expand Up @@ -143,6 +146,20 @@ impl Poller {
}
}

impl AsRawFd for Poller {
fn as_raw_fd(&self) -> RawFd {
self.port_fd
}
}

#[cfg(not(polling_no_io_safety))]
impl AsFd for Poller {
fn as_fd(&self) -> BorrowedFd<'_> {
// SAFETY: lifetime is bound by self
unsafe { BorrowedFd::new(self.port_fd) }
}
}

impl Drop for Poller {
fn drop(&mut self) {
let _ = self.delete(self.read_stream.as_raw_fd());
Expand Down
19 changes: 18 additions & 1 deletion src/wepoll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@
use std::convert::TryInto;
use std::io;
use std::os::windows::io::RawSocket;
use std::os::windows::io::{AsRawHandle, RawHandle, RawSocket};
use std::ptr;
use std::sync::atomic::{AtomicBool, Ordering};
use std::time::{Duration, Instant};

#[cfg(not(polling_no_io_safety))]
use std::os::windows::io::{AsHandle, BorrowedHandle};

use wepoll_ffi as we;
use winapi::ctypes;

Expand Down Expand Up @@ -166,6 +169,20 @@ impl Poller {
}
}

impl AsRawHandle for Poller {
fn as_raw_handle(&self) -> RawHandle {
self.handle as RawHandle
}
}

#[cfg(not(polling_no_io_safety))]
impl AsHandle for Poller {
fn as_handle(&self) -> BorrowedHandle<'_> {
// SAFETY: lifetime is bound by "self"
unsafe { BorrowedHandle::borrow_raw(self.as_raw_handle()) }
}
}

impl Drop for Poller {
fn drop(&mut self) {
log::trace!("drop: handle={:?}", self.handle);
Expand Down

0 comments on commit 323473e

Please sign in to comment.