Skip to content

Commit c93fe51

Browse files
authored
Implemented all std::os::fd traits for mqueue::MqdT. (#2097)
* Implemented all `std::os::fd` traits for `mqueue::MqdT`. * Updated CHANGELOG entry with pull request URL for #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.
1 parent b4836ea commit c93fe51

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ This project adheres to [Semantic Versioning](https://semver.org/).
5757
| RawFd | BorrowedFd or OwnedFd |
5858

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

6163
### Fixed
6264
- Fix: send `ETH_P_ALL` in htons format

src/mqueue.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,14 @@ use crate::sys::stat::Mode;
3737
use libc::{self, c_char, mqd_t, size_t};
3838
use std::ffi::CStr;
3939
use std::mem;
40+
#[cfg(any(
41+
target_os = "linux",
42+
target_os = "netbsd",
43+
target_os = "dragonfly"
44+
))]
45+
use std::os::unix::io::{
46+
AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, RawFd,
47+
};
4048

4149
libc_bitflags! {
4250
/// Used with [`mq_open`].
@@ -300,3 +308,43 @@ pub fn mq_remove_nonblock(mqd: &MqdT) -> Result<MqAttr> {
300308
);
301309
mq_setattr(mqd, &newattr)
302310
}
311+
312+
#[cfg(any(target_os = "linux", target_os = "netbsd", target_os = "dragonfly"))]
313+
impl AsFd for MqdT {
314+
/// Borrow the underlying message queue descriptor.
315+
fn as_fd(&self) -> BorrowedFd {
316+
// SAFETY: [MqdT] will only contain a valid fd by construction.
317+
unsafe { BorrowedFd::borrow_raw(self.0) }
318+
}
319+
}
320+
321+
#[cfg(any(target_os = "linux", target_os = "netbsd", target_os = "dragonfly"))]
322+
impl AsRawFd for MqdT {
323+
/// Return the underlying message queue descriptor.
324+
///
325+
/// Returned descriptor is a "shallow copy" of the descriptor, so it refers
326+
/// to the same underlying kernel object as `self`.
327+
fn as_raw_fd(&self) -> RawFd {
328+
self.0
329+
}
330+
}
331+
332+
#[cfg(any(target_os = "linux", target_os = "netbsd", target_os = "dragonfly"))]
333+
impl FromRawFd for MqdT {
334+
/// Construct an [MqdT] from [RawFd].
335+
///
336+
/// # Safety
337+
/// The `fd` given must be a valid and open file descriptor for a message
338+
/// queue.
339+
unsafe fn from_raw_fd(fd: RawFd) -> MqdT {
340+
MqdT(fd)
341+
}
342+
}
343+
344+
#[cfg(any(target_os = "linux", target_os = "netbsd", target_os = "dragonfly"))]
345+
impl IntoRawFd for MqdT {
346+
/// Consume this [MqdT] and return a [RawFd].
347+
fn into_raw_fd(self) -> RawFd {
348+
self.0
349+
}
350+
}

0 commit comments

Comments
 (0)