From a0613ba0e6900242b24af4f8fdb320e61fba8a3d Mon Sep 17 00:00:00 2001 From: Chris Date: Sat, 26 Aug 2023 18:30:36 +0000 Subject: [PATCH] Changed `name` parameter of `mqueue` functions to be generic over `NixPath`. (#2102) * Changed `name` parameter of `mq_open` and `mq_unlink` to be generic over `NixPath`. * Updated with PR link. * Fixed docs example code. * Fixed docs example code for real this time. --- CHANGELOG.md | 2 ++ src/mqueue.rs | 34 +++++++++++++++++++++------------- test/test_mq.rs | 16 +++++++--------- 3 files changed, 30 insertions(+), 22 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7d7a88d94c..32a603beba 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -63,6 +63,8 @@ This project adheres to [Semantic Versioning](https://semver.org/). (#[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)) +- Refactored `name` parameter of `mq_open` and `mq_unlink` to be generic over + `NixPath`. See ([#2102](https://github.com/nix-rust/nix/pull/2102)). ### Fixed - Fix: send `ETH_P_ALL` in htons format diff --git a/src/mqueue.rs b/src/mqueue.rs index e33797c81f..fb07d2accb 100644 --- a/src/mqueue.rs +++ b/src/mqueue.rs @@ -9,16 +9,16 @@ //! use nix::sys::stat::Mode; //! //! const MSG_SIZE: mq_attr_member_t = 32; -//! let mq_name= CString::new("/a_nix_test_queue").unwrap(); +//! let mq_name= "/a_nix_test_queue"; //! //! let oflag0 = MQ_OFlag::O_CREAT | MQ_OFlag::O_WRONLY; //! let mode = Mode::S_IWUSR | Mode::S_IRUSR | Mode::S_IRGRP | Mode::S_IROTH; -//! let mqd0 = mq_open(&mq_name, oflag0, mode, None).unwrap(); +//! let mqd0 = mq_open(mq_name, oflag0, mode, None).unwrap(); //! let msg_to_send = b"msg_1"; //! mq_send(&mqd0, msg_to_send, 1).unwrap(); //! //! let oflag1 = MQ_OFlag::O_CREAT | MQ_OFlag::O_RDONLY; -//! let mqd1 = mq_open(&mq_name, oflag1, mode, None).unwrap(); +//! let mqd1 = mq_open(mq_name, oflag1, mode, None).unwrap(); //! let mut buf = [0u8; 32]; //! let mut prio = 0u32; //! let len = mq_receive(&mqd1, &mut buf, &mut prio).unwrap(); @@ -31,11 +31,11 @@ //! [Further reading and details on the C API](https://man7.org/linux/man-pages/man7/mq_overview.7.html) use crate::errno::Errno; +use crate::NixPath; use crate::Result; 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", @@ -149,31 +149,39 @@ impl MqAttr { /// See also [`mq_open(2)`](https://pubs.opengroup.org/onlinepubs/9699919799/functions/mq_open.html) // The mode.bits() cast is only lossless on some OSes #[allow(clippy::cast_lossless)] -pub fn mq_open( - name: &CStr, +pub fn mq_open

( + name: &P, oflag: MQ_OFlag, mode: Mode, attr: Option<&MqAttr>, -) -> Result { - let res = match attr { +) -> Result +where + P: ?Sized + NixPath, +{ + let res = name.with_nix_path(|cstr| match attr { Some(mq_attr) => unsafe { libc::mq_open( - name.as_ptr(), + cstr.as_ptr(), oflag.bits(), mode.bits() as libc::c_int, &mq_attr.mq_attr as *const libc::mq_attr, ) }, - None => unsafe { libc::mq_open(name.as_ptr(), oflag.bits()) }, - }; + None => unsafe { libc::mq_open(cstr.as_ptr(), oflag.bits()) }, + })?; + Errno::result(res).map(MqdT) } /// Remove a message queue /// /// See also [`mq_unlink(2)`](https://pubs.opengroup.org/onlinepubs/9699919799/functions/mq_unlink.html) -pub fn mq_unlink(name: &CStr) -> Result<()> { - let res = unsafe { libc::mq_unlink(name.as_ptr()) }; +pub fn mq_unlink

(name: &P) -> Result<()> +where + P: ?Sized + NixPath, +{ + let res = + name.with_nix_path(|cstr| unsafe { libc::mq_unlink(cstr.as_ptr()) })?; Errno::result(res).map(drop) } diff --git a/test/test_mq.rs b/test/test_mq.rs index f232434e12..1fd8929c17 100644 --- a/test/test_mq.rs +++ b/test/test_mq.rs @@ -1,5 +1,4 @@ use cfg_if::cfg_if; -use std::ffi::CString; use std::str; use nix::errno::Errno; @@ -34,7 +33,7 @@ macro_rules! assert_attr_eq { fn test_mq_send_and_receive() { const MSG_SIZE: mq_attr_member_t = 32; let attr = MqAttr::new(0, 10, MSG_SIZE, 0); - let mq_name = &CString::new(b"/a_nix_test_queue".as_ref()).unwrap(); + let mq_name = "/a_nix_test_queue"; let oflag0 = MQ_OFlag::O_CREAT | MQ_OFlag::O_WRONLY; let mode = Mode::S_IWUSR | Mode::S_IRUSR | Mode::S_IRGRP | Mode::S_IROTH; @@ -63,7 +62,7 @@ fn test_mq_send_and_receive() { fn test_mq_timedreceive() { const MSG_SIZE: mq_attr_member_t = 32; let attr = MqAttr::new(0, 10, MSG_SIZE, 0); - let mq_name = &CString::new(b"/a_nix_test_queue".as_ref()).unwrap(); + let mq_name = "/a_nix_test_queue"; let oflag0 = MQ_OFlag::O_CREAT | MQ_OFlag::O_WRONLY; let mode = Mode::S_IWUSR | Mode::S_IRUSR | Mode::S_IRGRP | Mode::S_IROTH; @@ -95,7 +94,7 @@ fn test_mq_getattr() { use nix::mqueue::mq_getattr; const MSG_SIZE: mq_attr_member_t = 32; let initial_attr = MqAttr::new(0, 10, MSG_SIZE, 0); - let mq_name = &CString::new(b"/attr_test_get_attr".as_ref()).unwrap(); + let mq_name = "/attr_test_get_attr"; let oflag = MQ_OFlag::O_CREAT | MQ_OFlag::O_WRONLY; let mode = Mode::S_IWUSR | Mode::S_IRUSR | Mode::S_IRGRP | Mode::S_IROTH; let r = mq_open(mq_name, oflag, mode, Some(&initial_attr)); @@ -120,7 +119,7 @@ fn test_mq_setattr() { use nix::mqueue::{mq_getattr, mq_setattr}; const MSG_SIZE: mq_attr_member_t = 32; let initial_attr = MqAttr::new(0, 10, MSG_SIZE, 0); - let mq_name = &CString::new(b"/attr_test_get_attr".as_ref()).unwrap(); + let mq_name = "/attr_test_get_attr"; let oflag = MQ_OFlag::O_CREAT | MQ_OFlag::O_WRONLY; let mode = Mode::S_IWUSR | Mode::S_IRUSR | Mode::S_IRGRP | Mode::S_IROTH; let r = mq_open(mq_name, oflag, mode, Some(&initial_attr)); @@ -170,7 +169,7 @@ fn test_mq_set_nonblocking() { use nix::mqueue::{mq_getattr, mq_remove_nonblock, mq_set_nonblock}; const MSG_SIZE: mq_attr_member_t = 32; let initial_attr = MqAttr::new(0, 10, MSG_SIZE, 0); - let mq_name = &CString::new(b"/attr_test_get_attr".as_ref()).unwrap(); + let mq_name = "/attr_test_get_attr"; let oflag = MQ_OFlag::O_CREAT | MQ_OFlag::O_WRONLY; let mode = Mode::S_IWUSR | Mode::S_IRUSR | Mode::S_IRGRP | Mode::S_IROTH; let r = mq_open(mq_name, oflag, mode, Some(&initial_attr)); @@ -194,10 +193,9 @@ fn test_mq_unlink() { use nix::mqueue::mq_unlink; const MSG_SIZE: mq_attr_member_t = 32; let initial_attr = MqAttr::new(0, 10, MSG_SIZE, 0); - let mq_name_opened = &CString::new(b"/mq_unlink_test".as_ref()).unwrap(); + let mq_name_opened = "/mq_unlink_test"; #[cfg(not(any(target_os = "dragonfly", target_os = "netbsd")))] - let mq_name_not_opened = - &CString::new(b"/mq_unlink_test".as_ref()).unwrap(); + let mq_name_not_opened = "/mq_unlink_test"; let oflag = MQ_OFlag::O_CREAT | MQ_OFlag::O_WRONLY; let mode = Mode::S_IWUSR | Mode::S_IRUSR | Mode::S_IRGRP | Mode::S_IROTH; let r = mq_open(mq_name_opened, oflag, mode, Some(&initial_attr));