Skip to content

Commit 5f859d1

Browse files
bors[bot]asomers
andauthored
Merge #1759 #1760
1759: More docs for dir and mqueue r=rtzoeller a=asomers Add doc comments for the `dir` and `mqueue` modules. Also, delete dead code in `mqueue` 1760: Add const constructors for TimeSpec and TimeVal r=rtzoeller a=asomers These are basically the same as From<libc::timespec> and From<libc::timeval>, but they're const and require less typing. Co-authored-by: Alan Somers <[email protected]>
3 parents b1b4372 + 349f3ac + b3e4d59 commit 5f859d1

File tree

5 files changed

+54
-9
lines changed

5 files changed

+54
-9
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ This project adheres to [Semantic Versioning](https://semver.org/).
66
## [Unreleased] - ReleaseDate
77
### Added
88

9+
- Added const constructors for `TimeSpec` and `TimeVal`
10+
(#[1760](https://github.com/nix-rust/nix/pull/1760))
911
- Added `aio_writev` and `aio_readv`.
1012
(#[1713](https://github.com/nix-rust/nix/pull/1713))
11-
1213
- impl `From<uid_t>` for `Uid` and `From<gid_t>` for `Gid`
1314
(#[1727](https://github.com/nix-rust/nix/pull/1727))
1415
- impl From<SockaddrIn> for std::net::SocketAddrV4 and

src/dir.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//! List directory contents
2+
13
use crate::{Error, NixPath, Result};
24
use crate::errno::Errno;
35
use crate::fcntl::{self, OFlag};
@@ -114,6 +116,7 @@ fn next(dir: &mut Dir) -> Option<Result<Entry>> {
114116
}
115117
}
116118

119+
/// Return type of [`Dir::iter`].
117120
#[derive(Debug, Eq, Hash, PartialEq)]
118121
pub struct Iter<'d>(&'d mut Dir);
119122

@@ -183,14 +186,22 @@ impl IntoIterator for Dir {
183186
#[repr(transparent)]
184187
pub struct Entry(dirent);
185188

189+
/// Type of file referenced by a directory entry
186190
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)]
187191
pub enum Type {
192+
/// FIFO (Named pipe)
188193
Fifo,
194+
/// Character device
189195
CharacterDevice,
196+
/// Directory
190197
Directory,
198+
/// Block device
191199
BlockDevice,
200+
/// Regular file
192201
File,
202+
/// Symbolic link
193203
Symlink,
204+
/// Unix-domain socket
194205
Socket,
195206
}
196207

src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ mod macros;
6565
#[cfg(not(target_os = "redox"))]
6666
feature! {
6767
#![feature = "dir"]
68-
#[allow(missing_docs)]
6968
pub mod dir;
7069
}
7170
feature! {
@@ -119,7 +118,6 @@ feature! {
119118
))]
120119
feature! {
121120
#![feature = "mqueue"]
122-
#[allow(missing_docs)]
123121
pub mod mqueue;
124122
}
125123
feature! {

src/mqueue.rs

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,23 +39,29 @@ use crate::sys::stat::Mode;
3939
use std::mem;
4040

4141
libc_bitflags!{
42+
/// Used with [`mq_open`].
4243
pub struct MQ_OFlag: libc::c_int {
44+
/// Open the message queue for receiving messages.
4345
O_RDONLY;
46+
/// Open the queue for sending messages.
4447
O_WRONLY;
48+
/// Open the queue for both receiving and sending messages
4549
O_RDWR;
50+
/// Create a message queue.
4651
O_CREAT;
52+
/// If set along with `O_CREAT`, `mq_open` will fail if the message
53+
/// queue name exists.
4754
O_EXCL;
55+
/// `mq_send` and `mq_receive` should fail with `EAGAIN` rather than
56+
/// wait for resources that are not currently available.
4857
O_NONBLOCK;
58+
/// Set the close-on-exec flag for the message queue descriptor.
4959
O_CLOEXEC;
5060
}
5161
}
5262

53-
libc_bitflags!{
54-
pub struct FdFlag: libc::c_int {
55-
FD_CLOEXEC;
56-
}
57-
}
58-
63+
/// A message-queue attribute, optionally used with [`mq_setattr`] and
64+
/// [`mq_getattr`] and optionally [`mq_open`],
5965
#[repr(C)]
6066
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
6167
pub struct MqAttr {
@@ -72,14 +78,24 @@ pub struct MqdT(mqd_t);
7278

7379
// x32 compatibility
7480
// See https://sourceware.org/bugzilla/show_bug.cgi?id=21279
81+
/// Size of a message queue attribute member
7582
#[cfg(all(target_arch = "x86_64", target_pointer_width = "32"))]
7683
#[cfg_attr(docsrs, doc(cfg(all())))]
7784
pub type mq_attr_member_t = i64;
85+
/// Size of a message queue attribute member
7886
#[cfg(not(all(target_arch = "x86_64", target_pointer_width = "32")))]
7987
#[cfg_attr(docsrs, doc(cfg(all())))]
8088
pub type mq_attr_member_t = libc::c_long;
8189

8290
impl MqAttr {
91+
/// Create a new message queue attribute
92+
///
93+
/// # Arguments
94+
///
95+
/// - `mq_flags`: Either `0` or `O_NONBLOCK`.
96+
/// - `mq_maxmsg`: Maximum number of messages on the queue.
97+
/// - `mq_msgsize`: Maximum message size in bytes.
98+
/// - `mq_curmsgs`: Number of messages currently in the queue.
8399
pub fn new(mq_flags: mq_attr_member_t,
84100
mq_maxmsg: mq_attr_member_t,
85101
mq_msgsize: mq_attr_member_t,
@@ -97,6 +113,7 @@ impl MqAttr {
97113
}
98114
}
99115

116+
/// The current flags, either `0` or `O_NONBLOCK`.
100117
pub const fn flags(&self) -> mq_attr_member_t {
101118
self.mq_attr.mq_flags
102119
}

src/sys/time.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,15 @@ impl TimeValLike for TimeSpec {
330330
}
331331

332332
impl TimeSpec {
333+
/// Construct a new `TimeSpec` from its components
334+
#[cfg_attr(target_env = "musl", allow(deprecated))] // https://github.com/rust-lang/libc/issues/1848
335+
pub const fn new(seconds: time_t, nanoseconds: timespec_tv_nsec_t) -> Self {
336+
Self(timespec {
337+
tv_sec: seconds,
338+
tv_nsec: nanoseconds,
339+
})
340+
}
341+
333342
fn nanos_mod_sec(&self) -> timespec_tv_nsec_t {
334343
if self.tv_sec() < 0 && self.tv_nsec() > 0 {
335344
self.tv_nsec() - NANOS_PER_SEC as timespec_tv_nsec_t
@@ -564,6 +573,15 @@ impl TimeValLike for TimeVal {
564573
}
565574

566575
impl TimeVal {
576+
/// Construct a new `TimeVal` from its components
577+
#[cfg_attr(target_env = "musl", allow(deprecated))] // https://github.com/rust-lang/libc/issues/1848
578+
pub const fn new(seconds: time_t, microseconds: suseconds_t) -> Self {
579+
Self(timeval {
580+
tv_sec: seconds,
581+
tv_usec: microseconds,
582+
})
583+
}
584+
567585
fn micros_mod_sec(&self) -> suseconds_t {
568586
if self.tv_sec() < 0 && self.tv_usec() > 0 {
569587
self.tv_usec() - MICROS_PER_SEC as suseconds_t

0 commit comments

Comments
 (0)