From ed4334061d4336e2301ba6d4b31dad37c614dc37 Mon Sep 17 00:00:00 2001 From: finga Date: Sat, 14 Oct 2023 12:29:14 +0200 Subject: [PATCH] clippy: Fix several clippy issues In order to enable several clippy lint groups fix several findings. --- CHANGELOG.md | 14 +++++ Cargo.toml | 3 ++ src/lib.rs | 148 +++++++++++++++++++++------------------------------ 3 files changed, 78 insertions(+), 87 deletions(-) create mode 100644 CHANGELOG.md diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..eb9a1f0 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,14 @@ +# Changelog + +All notable changes to this project will be documented in this file. + +The format is based on [Keep a +Changelog](https://keepachangelog.com/en/1.0.0/), and this project +adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). + +## [Unreleased] + +### Changed + +- Fix several clippy findings in preperation to enable several lint + groups. diff --git a/Cargo.toml b/Cargo.toml index a2cee0f..1b7f1a1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,6 +5,9 @@ edition = "2018" authors = ["finga "] repository = "https://git.onders.org/finga/mqrs" license = "GPL-3.0-or-later" +description = "A simple API for SysV IPC message queues." +keywords = ["message_queue", "mq", "mqueue", "queue", "sysv", "ipc"] +categories = ["os"] [dependencies] libc = "0.2.98" diff --git a/src/lib.rs b/src/lib.rs index 298a48e..8d4cd96 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,6 +1,5 @@ use libc::{ - msgctl, msgget, msginfo, msqid_ds, IPC_CREAT, IPC_EXCL, IPC_INFO, IPC_NOWAIT, IPC_PRIVATE, - IPC_RMID, IPC_SET, IPC_STAT, MSG_COPY, MSG_EXCEPT, MSG_INFO, MSG_NOERROR, MSG_STAT, + msgctl, msgget, msqid_ds, IPC_CREAT, IPC_INFO, IPC_NOWAIT, IPC_RMID, MSG_INFO, MSG_STAT, }; use nix::errno::{errno, Errno}; use std::{marker::PhantomData, mem::MaybeUninit, ptr}; @@ -12,50 +11,15 @@ pub enum SysvMqError { ErrnoError(&'static str), } -/// IPC bit flags -#[repr(i32)] -pub enum Flags { - /// Create key if key does not exist. - CreateKey = IPC_CREAT, - /// Fail if key exists. - Exclusive = IPC_EXCL, - /// Return error on wait. - NoWait = IPC_NOWAIT, - /// No error if message is too big. - NoError = MSG_NOERROR, - /// Receive any message except of specified type. - Except = MSG_EXCEPT, - /// Copy (not remove) all queue messages. - Copy = MSG_COPY, - /// Private key (Special key value). - Private = IPC_PRIVATE, -} - -/// Commands for `msgctl()` -#[repr(i32)] -pub enum ControlCommands { - /// Remove identifier (Control command for `msgctl`, `semctl`, and `shmctl`). - Remove = IPC_RMID, - /// Set `ipc_perm` options (Control command for `msgctl`, `semctl`, and `shmctl`). - SetPerm = IPC_SET, - /// Get `ipc_perm` options (Control command for `msgctl`, `semctl`, and `shmctl`). - GetPerm = IPC_STAT, - /// See ipcs (Control command for `msgctl`, `semctl`, and `shmctl`). - IpcInfo = IPC_INFO, - /// IPCS control command. - Stat = MSG_STAT, - /// IPCS control command. - MsgInfo = MSG_INFO, -} - +#[allow(clippy::doc_markdown)] +/// Unlink (delete) an existing SysV IPC message queue. +/// +/// # Errors +/// +/// Return an `SysvMqError` when no queue with the given key can be +/// found. pub fn unlink_id(id: i32) -> Result<(), SysvMqError> { - let res = unsafe { - msgctl( - id, - ControlCommands::Remove as i32, - ptr::null::() as *mut msqid_ds, - ) - }; + let res = unsafe { msgctl(id, IPC_RMID, ptr::null::().cast_mut()) }; match res { -1 => Err(SysvMqError::ErrnoError(Errno::from_i32(errno()).desc())), @@ -63,6 +27,14 @@ pub fn unlink_id(id: i32) -> Result<(), SysvMqError> { } } +#[allow(clippy::doc_markdown)] +/// Get the id of an existing SysV IPC message queue by passing its +/// key. +/// +/// # Errors +/// +/// Return an `SysvMqError` when no queue with the given key can be +/// found. pub fn id_from_key(key: i32) -> Result { let id = unsafe { msgget(key, 0) }; @@ -72,54 +44,43 @@ pub fn id_from_key(key: i32) -> Result { } } -pub fn ipc_info(id: i32) -> Result<(), SysvMqError> { - let mut msginfo = MaybeUninit::::uninit(); +#[allow(clippy::doc_markdown)] +/// Get the info about an existing SysV IPC message queue. +pub fn ipc_info(id: i32) { + let mut ipc_info = MaybeUninit::::uninit(); - unsafe { - msgctl( - id, - ControlCommands::IpcInfo as i32, - msginfo.as_mut_ptr() as *mut msqid_ds, - ); - } + let ipc_info = unsafe { + msgctl(id, IPC_INFO, ipc_info.as_mut_ptr()); + ipc_info.assume_init() + }; - let msginfo = unsafe { msginfo.assume_init() }; - - println!("info: {:?}", msginfo); - - Ok(()) + println!("info: {ipc_info:?}"); } -pub fn stat_info(id: i32) -> Result<(), SysvMqError> { - let mut msginfo = MaybeUninit::::uninit(); +#[allow(clippy::doc_markdown)] +/// Get the stats about an existing SysV IPC message queue. +pub fn stat_info(id: i32) { + let mut stat_info = MaybeUninit::::uninit(); - unsafe { - msgctl(id, ControlCommands::Stat as i32, msginfo.as_mut_ptr()); - } + let stat_info = unsafe { + msgctl(id, MSG_STAT, stat_info.as_mut_ptr()); + stat_info.assume_init() + }; - let msginfo = unsafe { msginfo.assume_init() }; - - println!("info: {:?}", msginfo); - - Ok(()) + println!("info: {stat_info:?}"); } -pub fn msg_info(id: i32) -> Result<(), SysvMqError> { - let mut msginfo = MaybeUninit::::uninit(); +#[allow(clippy::doc_markdown)] +/// Get the message info about an existing SysV IPC message queue. +pub fn msg_info(id: i32) { + let mut msg_info = MaybeUninit::::uninit(); - unsafe { - msgctl( - id, - ControlCommands::MsgInfo as i32, - msginfo.as_mut_ptr() as *mut msqid_ds, - ); - } + let msg_info = unsafe { + msgctl(id, MSG_INFO, msg_info.as_mut_ptr()); + msg_info.assume_init() + }; - let msginfo = unsafe { msginfo.assume_init() }; - - println!("info: {:?}", msginfo); - - Ok(()) + println!("info: {msg_info:?}"); } pub struct SysvMq { @@ -131,9 +92,15 @@ pub struct SysvMq { } impl SysvMq { + /// Create a new message queye with the given key. + /// + /// # Errors + /// + /// Return an `SysvMqError` when no queue with the given key can be + /// created. pub fn create(&mut self, key: i32) -> Result<&Self, SysvMqError> { self.key = key; - self.id = unsafe { msgget(self.key, Flags::CreateKey as i32 | self.mode) }; + self.id = unsafe { msgget(self.key, IPC_CREAT | self.mode) }; match self.id { -1 => Err(SysvMqError::ErrnoError(Errno::from_i32(errno()).desc())), @@ -141,6 +108,12 @@ impl SysvMq { } } + /// Open an existing message queye with the given key. + /// + /// # Errors + /// + /// Return an `SysvMqError` when no queue with the given key can be + /// found. pub fn open(mut self, key: i32) -> Result { self.key = key; self.id = unsafe { msgget(self.key, self.mode) }; @@ -157,12 +130,13 @@ impl SysvMq { } pub fn nonblocking(&mut self) -> &Self { - self.message_mask |= Flags::NoWait as i32; + self.message_mask |= IPC_NOWAIT; self } - pub fn new() -> Self { - SysvMq { + #[must_use] + pub const fn new() -> Self { + Self { id: -1, key: 0, message_mask: 0,