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

Implemented all std::os::fd traits for mqueue::MqdT. #2097

Merged
merged 10 commits into from
Aug 19, 2023
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ This project adheres to [Semantic Versioning](https://semver.org/).
| RawFd | BorrowedFd or OwnedFd |

(#[1906](https://github.com/nix-rust/nix/pull/1906))
- Implemented AsFd, AsRawFd, FromRawFd, and IntoRawFd for `mqueue::MqdT`.
See ([#2097](https://github.com/nix-rust/nix/pull/2097))

### Fixed
- Fix: send `ETH_P_ALL` in htons format
Expand Down
59 changes: 59 additions & 0 deletions src/mqueue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ use crate::sys::stat::Mode;
use libc::{self, c_char, mqd_t, size_t};
use std::ffi::CStr;
use std::mem;
#[cfg(all(
unix,
any(target_os = "linux", target_os = "netbsd", target_os = "dragonfly")
))]
use std::os::unix::io::{
AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, RawFd,
};

libc_bitflags! {
/// Used with [`mq_open`].
Expand Down Expand Up @@ -300,3 +307,55 @@ pub fn mq_remove_nonblock(mqd: &MqdT) -> Result<MqAttr> {
);
mq_setattr(mqd, &newattr)
}

#[cfg(all(
unix,
any(target_os = "linux", target_os = "netbsd", target_os = "dragonfly")
))]
impl AsFd for MqdT {
/// Borrow the underlying message queue descriptor.
fn as_fd(&self) -> BorrowedFd {
// SAFETY: [MqdT] will only contain a valid fd by construction.
unsafe { BorrowedFd::borrow_raw(self.0) }
}
}

#[cfg(all(
unix,
coderBlitz marked this conversation as resolved.
Show resolved Hide resolved
any(target_os = "linux", target_os = "netbsd", target_os = "dragonfly")
))]
impl AsRawFd for MqdT {
/// Return the underlying message queue descriptor.
///
/// Returned descriptor is a "shallow copy" of the descriptor, so it refers
/// to the same underlying kernel object as `self`.
fn as_raw_fd(&self) -> RawFd {
self.0
}
}

#[cfg(all(
unix,
any(target_os = "linux", target_os = "netbsd", target_os = "dragonfly")
))]
impl FromRawFd for MqdT {
/// Construct an [MqdT] from [RawFd].
///
/// # Safety
/// The `fd` given must be a valid and open file descriptor for a message
/// queue.
unsafe fn from_raw_fd(fd: RawFd) -> MqdT {
MqdT(fd)
}
}

#[cfg(all(
unix,
any(target_os = "linux", target_os = "netbsd", target_os = "dragonfly")
))]
impl IntoRawFd for MqdT {
/// Consume this [MqdT] and return a [RawFd].
fn into_raw_fd(self) -> RawFd {
self.0
}
}