Skip to content

Commit 8a6754e

Browse files
committed
Breaking change: support creating an Async with AsFd rather than AsRawFd
Modify the `Async::new` constructor to use `AsFd` rather than `AsRawFd` (or the socket equivalents on Windows). Modify other code accordingly. Drop impls of `AsRawFd` and `AsRawSocket`.
1 parent 090ee6b commit 8a6754e

File tree

2 files changed

+21
-35
lines changed

2 files changed

+21
-35
lines changed

examples/unix-signal.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,21 @@
88
99
#[cfg(unix)]
1010
fn main() -> std::io::Result<()> {
11-
use std::os::unix::{io::AsRawFd, net::UnixStream};
11+
use std::os::unix::{
12+
io::{AsFd, AsRawFd},
13+
net::UnixStream,
14+
};
1215

1316
use async_io::Async;
1417
use futures_lite::{future, prelude::*};
1518

1619
future::block_on(async {
1720
// Create a Unix stream that receives a byte on each signal occurrence.
1821
let (a, mut b) = Async::<UnixStream>::pair()?;
19-
signal_hook::low_level::pipe::register_raw(signal_hook::consts::SIGINT, a.as_raw_fd())?;
22+
signal_hook::low_level::pipe::register_raw(
23+
signal_hook::consts::SIGINT,
24+
a.as_fd().as_raw_fd(),
25+
)?;
2026
println!("Waiting for Ctrl-C...");
2127

2228
// Receive a byte that indicates the Ctrl-C signal occurred.

src/lib.rs

Lines changed: 13 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ use std::time::{Duration, Instant};
6666

6767
#[cfg(unix)]
6868
use std::{
69-
os::unix::io::{AsFd, AsRawFd, BorrowedFd, OwnedFd, RawFd},
69+
os::unix::io::{AsFd, AsRawFd, BorrowedFd, OwnedFd},
7070
os::unix::net::{SocketAddr as UnixSocketAddr, UnixDatagram, UnixListener, UnixStream},
7171
path::Path,
7272
};
@@ -621,14 +621,14 @@ pub struct Async<T> {
621621
impl<T> Unpin for Async<T> {}
622622

623623
#[cfg(unix)]
624-
impl<T: AsRawFd> Async<T> {
624+
impl<T: AsFd> Async<T> {
625625
/// Creates an async I/O handle.
626626
///
627627
/// This method will put the handle in non-blocking mode and register it in
628628
/// [epoll]/[kqueue]/[event ports]/[IOCP].
629629
///
630-
/// On Unix systems, the handle must implement `AsRawFd`, while on Windows it must implement
631-
/// `AsRawSocket`.
630+
/// On Unix systems, the handle must implement `AsFd`, while on Windows it must implement
631+
/// `AsSocket`.
632632
///
633633
/// [epoll]: https://en.wikipedia.org/wiki/Epoll
634634
/// [kqueue]: https://en.wikipedia.org/wiki/Kqueue
@@ -647,14 +647,9 @@ impl<T: AsRawFd> Async<T> {
647647
/// # std::io::Result::Ok(()) });
648648
/// ```
649649
pub fn new(io: T) -> io::Result<Async<T>> {
650-
let raw = io.as_raw_fd();
650+
let fd = io.as_fd();
651651

652652
// Put the file descriptor in non-blocking mode.
653-
//
654-
// Safety: We assume `as_raw_fd()` returns a valid fd. When we can
655-
// depend on Rust >= 1.63, where `AsFd` is stabilized, and when
656-
// `TimerFd` implements it, we can remove this unsafe and simplify this.
657-
let fd = unsafe { rustix::fd::BorrowedFd::borrow_raw(raw) };
658653
cfg_if::cfg_if! {
659654
// ioctl(FIONBIO) sets the flag atomically, but we use this only on Linux
660655
// for now, as with the standard library, because it seems to behave
@@ -674,19 +669,12 @@ impl<T: AsRawFd> Async<T> {
674669
}
675670

676671
Ok(Async {
677-
source: Reactor::get().insert_io(raw)?,
672+
source: Reactor::get().insert_io(fd.as_raw_fd())?,
678673
io: Some(io),
679674
})
680675
}
681676
}
682677

683-
#[cfg(unix)]
684-
impl<T: AsRawFd> AsRawFd for Async<T> {
685-
fn as_raw_fd(&self) -> RawFd {
686-
self.get_ref().as_raw_fd()
687-
}
688-
}
689-
690678
#[cfg(unix)]
691679
impl<T: AsFd> AsFd for Async<T> {
692680
fn as_fd(&self) -> BorrowedFd<'_> {
@@ -695,7 +683,7 @@ impl<T: AsFd> AsFd for Async<T> {
695683
}
696684

697685
#[cfg(unix)]
698-
impl<T: AsRawFd + From<OwnedFd>> TryFrom<OwnedFd> for Async<T> {
686+
impl<T: AsFd + From<OwnedFd>> TryFrom<OwnedFd> for Async<T> {
699687
type Error = io::Error;
700688

701689
fn try_from(value: OwnedFd) -> Result<Self, Self::Error> {
@@ -713,14 +701,14 @@ impl<T: Into<OwnedFd>> TryFrom<Async<T>> for OwnedFd {
713701
}
714702

715703
#[cfg(windows)]
716-
impl<T: AsRawSocket> Async<T> {
704+
impl<T: AsSocket> Async<T> {
717705
/// Creates an async I/O handle.
718706
///
719707
/// This method will put the handle in non-blocking mode and register it in
720708
/// [epoll]/[kqueue]/[event ports]/[IOCP].
721709
///
722-
/// On Unix systems, the handle must implement `AsRawFd`, while on Windows it must implement
723-
/// `AsRawSocket`.
710+
/// On Unix systems, the handle must implement `AsFd`, while on Windows it must implement
711+
/// `AsSocket`.
724712
///
725713
/// [epoll]: https://en.wikipedia.org/wiki/Epoll
726714
/// [kqueue]: https://en.wikipedia.org/wiki/Kqueue
@@ -739,8 +727,7 @@ impl<T: AsRawSocket> Async<T> {
739727
/// # std::io::Result::Ok(()) });
740728
/// ```
741729
pub fn new(io: T) -> io::Result<Async<T>> {
742-
let sock = io.as_raw_socket();
743-
let borrowed = unsafe { rustix::fd::BorrowedFd::borrow_raw(sock) };
730+
let borrowed = io.as_socket();
744731

745732
// Put the socket in non-blocking mode.
746733
//
@@ -750,19 +737,12 @@ impl<T: AsRawSocket> Async<T> {
750737
rustix::io::ioctl_fionbio(borrowed, true)?;
751738

752739
Ok(Async {
753-
source: Reactor::get().insert_io(sock)?,
740+
source: Reactor::get().insert_io(borrowed.as_raw_socket())?,
754741
io: Some(io),
755742
})
756743
}
757744
}
758745

759-
#[cfg(windows)]
760-
impl<T: AsRawSocket> AsRawSocket for Async<T> {
761-
fn as_raw_socket(&self) -> RawSocket {
762-
self.get_ref().as_raw_socket()
763-
}
764-
}
765-
766746
#[cfg(windows)]
767747
impl<T: AsSocket> AsSocket for Async<T> {
768748
fn as_socket(&self) -> BorrowedSocket<'_> {
@@ -771,7 +751,7 @@ impl<T: AsSocket> AsSocket for Async<T> {
771751
}
772752

773753
#[cfg(windows)]
774-
impl<T: AsRawSocket + From<OwnedSocket>> TryFrom<OwnedSocket> for Async<T> {
754+
impl<T: AsSocket + From<OwnedSocket>> TryFrom<OwnedSocket> for Async<T> {
775755
type Error = io::Error;
776756

777757
fn try_from(value: OwnedSocket) -> Result<Self, Self::Error> {

0 commit comments

Comments
 (0)