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

Implement AsFd and bidirectional conversion to/from OwnedFd #202

Merged
merged 1 commit into from
Jun 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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()) }
}
}