Skip to content

Commit

Permalink
Implemented all std::os::fd traits for mqueue::MqdT. (nix-rust#2097)
Browse files Browse the repository at this point in the history
* Implemented all `std::os::fd` traits for `mqueue::MqdT`.

* Updated CHANGELOG entry with pull request URL for nix-rust#2097.

* Bumped the minimum rustc version to match that of the `std::os::fd` module.

* MSRV bumped to 1.66, including cirrus configs.

* Refactored use statement to reduce MSRV back to 1.63.

* Converted indents to spaces.

* Updated last changes for rust formatter CI.

* Actual last format change this time.

* Added NetBSD and DragonflyBSD build support.

* Removed redundant  cfg condition.
  • Loading branch information
coderBlitz committed Aug 19, 2023
1 parent b4836ea commit c93fe51
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
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
48 changes: 48 additions & 0 deletions src/mqueue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ use crate::sys::stat::Mode;
use libc::{self, c_char, mqd_t, size_t};
use std::ffi::CStr;
use std::mem;
#[cfg(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 +308,43 @@ pub fn mq_remove_nonblock(mqd: &MqdT) -> Result<MqAttr> {
);
mq_setattr(mqd, &newattr)
}

#[cfg(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(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(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(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
}
}

0 comments on commit c93fe51

Please sign in to comment.