Skip to content

Commit

Permalink
Implement AsFd and bidirectional conversion to/from OwnedFd
Browse files Browse the repository at this point in the history
In particular, this makes it possible to get a `BorrowedFd` from an
`Inotify`, which allows integration with polling mechanisms that don't
use `RawFd`. This will be needed to work with the next version of
`async-io`.
  • Loading branch information
joshtriplett committed Jun 7, 2023
1 parent 9ec50c5 commit 3f2d7e0
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
- name: Install rust
uses: actions-rs/toolchain@v1
with:
toolchain: 1.49.0
toolchain: 1.63.0
override: true
components: clippy
- name: Check rust and cargo version
Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ authors = [
"Frank Denis <github@pureftpd.org>"
]
edition = "2018"
rust-version = "1.63"

description = "Idiomatic wrapper for inotify"
documentation = "https://docs.rs/inotify"
Expand Down
9 changes: 9 additions & 0 deletions src/fd_guard.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use std::{
ops::Deref,
os::unix::io::{
AsFd,
AsRawFd,
BorrowedFd,
FromRawFd,
IntoRawFd,
RawFd,
Expand Down Expand Up @@ -77,6 +79,13 @@ impl AsRawFd for FdGuard {
}
}

impl AsFd for FdGuard {
#[inline]
fn as_fd(&self) -> BorrowedFd<'_> {
unsafe { BorrowedFd::borrow_raw(self.fd) }
}
}

impl PartialEq for FdGuard {
fn eq(&self, other: &FdGuard) -> bool {
self.fd == other.fd
Expand Down
22 changes: 22 additions & 0 deletions src/inotify.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
use std::{
io,
os::unix::io::{
AsFd,
AsRawFd,
BorrowedFd,
FromRawFd,
IntoRawFd,
OwnedFd,
RawFd,
},
path::Path,
Expand Down Expand Up @@ -370,3 +373,22 @@ impl IntoRawFd for Inotify {
self.fd.fd
}
}

impl AsFd for Inotify {
#[inline]
fn as_fd(&self) -> BorrowedFd<'_> {
self.fd.as_fd()
}
}

impl From<Inotify> for OwnedFd {
fn from(fd: Inotify) -> OwnedFd {
unsafe { OwnedFd::from_raw_fd(fd.into_raw_fd()) }
}
}

impl From<OwnedFd> for Inotify {
fn from(fd: OwnedFd) -> Inotify {
unsafe { Inotify::from_raw_fd(fd.into_raw_fd()) }
}
}

0 comments on commit 3f2d7e0

Please sign in to comment.