Skip to content

Commit b5bc5ae

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`. Update timerfd dev-dependency to 1.5 so that the timerfd example still builds.
1 parent c72bf7f commit b5bc5ae

File tree

3 files changed

+22
-36
lines changed

3 files changed

+22
-36
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ tempfile = "3"
4747

4848
[target.'cfg(target_os = "linux")'.dev-dependencies]
4949
inotify = { version = "0.10.1", default-features = false }
50-
timerfd = "1"
50+
timerfd = "1.5"
5151

5252
[target.'cfg(windows)'.dev-dependencies]
5353
uds_windows = "1"

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
};
@@ -615,14 +615,14 @@ pub struct Async<T> {
615615
impl<T> Unpin for Async<T> {}
616616

617617
#[cfg(unix)]
618-
impl<T: AsRawFd> Async<T> {
618+
impl<T: AsFd> Async<T> {
619619
/// Creates an async I/O handle.
620620
///
621621
/// This method will put the handle in non-blocking mode and register it in
622622
/// [epoll]/[kqueue]/[event ports]/[IOCP].
623623
///
624-
/// On Unix systems, the handle must implement `AsRawFd`, while on Windows it must implement
625-
/// `AsRawSocket`.
624+
/// On Unix systems, the handle must implement `AsFd`, while on Windows it must implement
625+
/// `AsSocket`.
626626
///
627627
/// [epoll]: https://en.wikipedia.org/wiki/Epoll
628628
/// [kqueue]: https://en.wikipedia.org/wiki/Kqueue
@@ -641,14 +641,9 @@ impl<T: AsRawFd> Async<T> {
641641
/// # std::io::Result::Ok(()) });
642642
/// ```
643643
pub fn new(io: T) -> io::Result<Async<T>> {
644-
let raw = io.as_raw_fd();
644+
let fd = io.as_fd();
645645

646646
// Put the file descriptor in non-blocking mode.
647-
//
648-
// Safety: We assume `as_raw_fd()` returns a valid fd. When we can
649-
// depend on Rust >= 1.63, where `AsFd` is stabilized, and when
650-
// `TimerFd` implements it, we can remove this unsafe and simplify this.
651-
let fd = unsafe { rustix::fd::BorrowedFd::borrow_raw(raw) };
652647
cfg_if::cfg_if! {
653648
// ioctl(FIONBIO) sets the flag atomically, but we use this only on Linux
654649
// for now, as with the standard library, because it seems to behave
@@ -668,19 +663,12 @@ impl<T: AsRawFd> Async<T> {
668663
}
669664

670665
Ok(Async {
671-
source: Reactor::get().insert_io(raw)?,
666+
source: Reactor::get().insert_io(fd.as_raw_fd())?,
672667
io: Some(io),
673668
})
674669
}
675670
}
676671

677-
#[cfg(unix)]
678-
impl<T: AsRawFd> AsRawFd for Async<T> {
679-
fn as_raw_fd(&self) -> RawFd {
680-
self.get_ref().as_raw_fd()
681-
}
682-
}
683-
684672
#[cfg(unix)]
685673
impl<T: AsFd> AsFd for Async<T> {
686674
fn as_fd(&self) -> BorrowedFd<'_> {
@@ -689,7 +677,7 @@ impl<T: AsFd> AsFd for Async<T> {
689677
}
690678

691679
#[cfg(unix)]
692-
impl<T: AsRawFd + From<OwnedFd>> TryFrom<OwnedFd> for Async<T> {
680+
impl<T: AsFd + From<OwnedFd>> TryFrom<OwnedFd> for Async<T> {
693681
type Error = io::Error;
694682

695683
fn try_from(value: OwnedFd) -> Result<Self, Self::Error> {
@@ -707,14 +695,14 @@ impl<T: Into<OwnedFd>> TryFrom<Async<T>> for OwnedFd {
707695
}
708696

709697
#[cfg(windows)]
710-
impl<T: AsRawSocket> Async<T> {
698+
impl<T: AsSocket> Async<T> {
711699
/// Creates an async I/O handle.
712700
///
713701
/// This method will put the handle in non-blocking mode and register it in
714702
/// [epoll]/[kqueue]/[event ports]/[IOCP].
715703
///
716-
/// On Unix systems, the handle must implement `AsRawFd`, while on Windows it must implement
717-
/// `AsRawSocket`.
704+
/// On Unix systems, the handle must implement `AsFd`, while on Windows it must implement
705+
/// `AsSocket`.
718706
///
719707
/// [epoll]: https://en.wikipedia.org/wiki/Epoll
720708
/// [kqueue]: https://en.wikipedia.org/wiki/Kqueue
@@ -733,8 +721,7 @@ impl<T: AsRawSocket> Async<T> {
733721
/// # std::io::Result::Ok(()) });
734722
/// ```
735723
pub fn new(io: T) -> io::Result<Async<T>> {
736-
let sock = io.as_raw_socket();
737-
let borrowed = unsafe { rustix::fd::BorrowedFd::borrow_raw(sock) };
724+
let borrowed = io.as_socket();
738725

739726
// Put the socket in non-blocking mode.
740727
//
@@ -744,19 +731,12 @@ impl<T: AsRawSocket> Async<T> {
744731
rustix::io::ioctl_fionbio(borrowed, true)?;
745732

746733
Ok(Async {
747-
source: Reactor::get().insert_io(sock)?,
734+
source: Reactor::get().insert_io(borrowed.as_raw_socket())?,
748735
io: Some(io),
749736
})
750737
}
751738
}
752739

753-
#[cfg(windows)]
754-
impl<T: AsRawSocket> AsRawSocket for Async<T> {
755-
fn as_raw_socket(&self) -> RawSocket {
756-
self.get_ref().as_raw_socket()
757-
}
758-
}
759-
760740
#[cfg(windows)]
761741
impl<T: AsSocket> AsSocket for Async<T> {
762742
fn as_socket(&self) -> BorrowedSocket<'_> {
@@ -765,7 +745,7 @@ impl<T: AsSocket> AsSocket for Async<T> {
765745
}
766746

767747
#[cfg(windows)]
768-
impl<T: AsRawSocket + From<OwnedSocket>> TryFrom<OwnedSocket> for Async<T> {
748+
impl<T: AsSocket + From<OwnedSocket>> TryFrom<OwnedSocket> for Async<T> {
769749
type Error = io::Error;
770750

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

0 commit comments

Comments
 (0)