Skip to content

Commit a0613ba

Browse files
authored
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.
1 parent 733ddf1 commit a0613ba

File tree

3 files changed

+30
-22
lines changed

3 files changed

+30
-22
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ This project adheres to [Semantic Versioning](https://semver.org/).
6363
(#[1906](https://github.com/nix-rust/nix/pull/1906))
6464
- Implemented AsFd, AsRawFd, FromRawFd, and IntoRawFd for `mqueue::MqdT`.
6565
See ([#2097](https://github.com/nix-rust/nix/pull/2097))
66+
- Refactored `name` parameter of `mq_open` and `mq_unlink` to be generic over
67+
`NixPath`. See ([#2102](https://github.com/nix-rust/nix/pull/2102)).
6668

6769
### Fixed
6870
- Fix: send `ETH_P_ALL` in htons format

src/mqueue.rs

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,16 @@
99
//! use nix::sys::stat::Mode;
1010
//!
1111
//! const MSG_SIZE: mq_attr_member_t = 32;
12-
//! let mq_name= CString::new("/a_nix_test_queue").unwrap();
12+
//! let mq_name= "/a_nix_test_queue";
1313
//!
1414
//! let oflag0 = MQ_OFlag::O_CREAT | MQ_OFlag::O_WRONLY;
1515
//! let mode = Mode::S_IWUSR | Mode::S_IRUSR | Mode::S_IRGRP | Mode::S_IROTH;
16-
//! let mqd0 = mq_open(&mq_name, oflag0, mode, None).unwrap();
16+
//! let mqd0 = mq_open(mq_name, oflag0, mode, None).unwrap();
1717
//! let msg_to_send = b"msg_1";
1818
//! mq_send(&mqd0, msg_to_send, 1).unwrap();
1919
//!
2020
//! let oflag1 = MQ_OFlag::O_CREAT | MQ_OFlag::O_RDONLY;
21-
//! let mqd1 = mq_open(&mq_name, oflag1, mode, None).unwrap();
21+
//! let mqd1 = mq_open(mq_name, oflag1, mode, None).unwrap();
2222
//! let mut buf = [0u8; 32];
2323
//! let mut prio = 0u32;
2424
//! let len = mq_receive(&mqd1, &mut buf, &mut prio).unwrap();
@@ -31,11 +31,11 @@
3131
//! [Further reading and details on the C API](https://man7.org/linux/man-pages/man7/mq_overview.7.html)
3232
3333
use crate::errno::Errno;
34+
use crate::NixPath;
3435
use crate::Result;
3536

3637
use crate::sys::stat::Mode;
3738
use libc::{self, c_char, mqd_t, size_t};
38-
use std::ffi::CStr;
3939
use std::mem;
4040
#[cfg(any(
4141
target_os = "linux",
@@ -149,31 +149,39 @@ impl MqAttr {
149149
/// See also [`mq_open(2)`](https://pubs.opengroup.org/onlinepubs/9699919799/functions/mq_open.html)
150150
// The mode.bits() cast is only lossless on some OSes
151151
#[allow(clippy::cast_lossless)]
152-
pub fn mq_open(
153-
name: &CStr,
152+
pub fn mq_open<P>(
153+
name: &P,
154154
oflag: MQ_OFlag,
155155
mode: Mode,
156156
attr: Option<&MqAttr>,
157-
) -> Result<MqdT> {
158-
let res = match attr {
157+
) -> Result<MqdT>
158+
where
159+
P: ?Sized + NixPath,
160+
{
161+
let res = name.with_nix_path(|cstr| match attr {
159162
Some(mq_attr) => unsafe {
160163
libc::mq_open(
161-
name.as_ptr(),
164+
cstr.as_ptr(),
162165
oflag.bits(),
163166
mode.bits() as libc::c_int,
164167
&mq_attr.mq_attr as *const libc::mq_attr,
165168
)
166169
},
167-
None => unsafe { libc::mq_open(name.as_ptr(), oflag.bits()) },
168-
};
170+
None => unsafe { libc::mq_open(cstr.as_ptr(), oflag.bits()) },
171+
})?;
172+
169173
Errno::result(res).map(MqdT)
170174
}
171175

172176
/// Remove a message queue
173177
///
174178
/// See also [`mq_unlink(2)`](https://pubs.opengroup.org/onlinepubs/9699919799/functions/mq_unlink.html)
175-
pub fn mq_unlink(name: &CStr) -> Result<()> {
176-
let res = unsafe { libc::mq_unlink(name.as_ptr()) };
179+
pub fn mq_unlink<P>(name: &P) -> Result<()>
180+
where
181+
P: ?Sized + NixPath,
182+
{
183+
let res =
184+
name.with_nix_path(|cstr| unsafe { libc::mq_unlink(cstr.as_ptr()) })?;
177185
Errno::result(res).map(drop)
178186
}
179187

test/test_mq.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use cfg_if::cfg_if;
2-
use std::ffi::CString;
32
use std::str;
43

54
use nix::errno::Errno;
@@ -34,7 +33,7 @@ macro_rules! assert_attr_eq {
3433
fn test_mq_send_and_receive() {
3534
const MSG_SIZE: mq_attr_member_t = 32;
3635
let attr = MqAttr::new(0, 10, MSG_SIZE, 0);
37-
let mq_name = &CString::new(b"/a_nix_test_queue".as_ref()).unwrap();
36+
let mq_name = "/a_nix_test_queue";
3837

3938
let oflag0 = MQ_OFlag::O_CREAT | MQ_OFlag::O_WRONLY;
4039
let mode = Mode::S_IWUSR | Mode::S_IRUSR | Mode::S_IRGRP | Mode::S_IROTH;
@@ -63,7 +62,7 @@ fn test_mq_send_and_receive() {
6362
fn test_mq_timedreceive() {
6463
const MSG_SIZE: mq_attr_member_t = 32;
6564
let attr = MqAttr::new(0, 10, MSG_SIZE, 0);
66-
let mq_name = &CString::new(b"/a_nix_test_queue".as_ref()).unwrap();
65+
let mq_name = "/a_nix_test_queue";
6766

6867
let oflag0 = MQ_OFlag::O_CREAT | MQ_OFlag::O_WRONLY;
6968
let mode = Mode::S_IWUSR | Mode::S_IRUSR | Mode::S_IRGRP | Mode::S_IROTH;
@@ -95,7 +94,7 @@ fn test_mq_getattr() {
9594
use nix::mqueue::mq_getattr;
9695
const MSG_SIZE: mq_attr_member_t = 32;
9796
let initial_attr = MqAttr::new(0, 10, MSG_SIZE, 0);
98-
let mq_name = &CString::new(b"/attr_test_get_attr".as_ref()).unwrap();
97+
let mq_name = "/attr_test_get_attr";
9998
let oflag = MQ_OFlag::O_CREAT | MQ_OFlag::O_WRONLY;
10099
let mode = Mode::S_IWUSR | Mode::S_IRUSR | Mode::S_IRGRP | Mode::S_IROTH;
101100
let r = mq_open(mq_name, oflag, mode, Some(&initial_attr));
@@ -120,7 +119,7 @@ fn test_mq_setattr() {
120119
use nix::mqueue::{mq_getattr, mq_setattr};
121120
const MSG_SIZE: mq_attr_member_t = 32;
122121
let initial_attr = MqAttr::new(0, 10, MSG_SIZE, 0);
123-
let mq_name = &CString::new(b"/attr_test_get_attr".as_ref()).unwrap();
122+
let mq_name = "/attr_test_get_attr";
124123
let oflag = MQ_OFlag::O_CREAT | MQ_OFlag::O_WRONLY;
125124
let mode = Mode::S_IWUSR | Mode::S_IRUSR | Mode::S_IRGRP | Mode::S_IROTH;
126125
let r = mq_open(mq_name, oflag, mode, Some(&initial_attr));
@@ -170,7 +169,7 @@ fn test_mq_set_nonblocking() {
170169
use nix::mqueue::{mq_getattr, mq_remove_nonblock, mq_set_nonblock};
171170
const MSG_SIZE: mq_attr_member_t = 32;
172171
let initial_attr = MqAttr::new(0, 10, MSG_SIZE, 0);
173-
let mq_name = &CString::new(b"/attr_test_get_attr".as_ref()).unwrap();
172+
let mq_name = "/attr_test_get_attr";
174173
let oflag = MQ_OFlag::O_CREAT | MQ_OFlag::O_WRONLY;
175174
let mode = Mode::S_IWUSR | Mode::S_IRUSR | Mode::S_IRGRP | Mode::S_IROTH;
176175
let r = mq_open(mq_name, oflag, mode, Some(&initial_attr));
@@ -194,10 +193,9 @@ fn test_mq_unlink() {
194193
use nix::mqueue::mq_unlink;
195194
const MSG_SIZE: mq_attr_member_t = 32;
196195
let initial_attr = MqAttr::new(0, 10, MSG_SIZE, 0);
197-
let mq_name_opened = &CString::new(b"/mq_unlink_test".as_ref()).unwrap();
196+
let mq_name_opened = "/mq_unlink_test";
198197
#[cfg(not(any(target_os = "dragonfly", target_os = "netbsd")))]
199-
let mq_name_not_opened =
200-
&CString::new(b"/mq_unlink_test".as_ref()).unwrap();
198+
let mq_name_not_opened = "/mq_unlink_test";
201199
let oflag = MQ_OFlag::O_CREAT | MQ_OFlag::O_WRONLY;
202200
let mode = Mode::S_IWUSR | Mode::S_IRUSR | Mode::S_IRGRP | Mode::S_IROTH;
203201
let r = mq_open(mq_name_opened, oflag, mode, Some(&initial_attr));

0 commit comments

Comments
 (0)