Skip to content

Commit e8bcc80

Browse files
authored
Add more documentation for inotify. (#1140)
Add a simple module-level example for inotify, to show how to get started, and to demonstrate the `use rustix::fs::inotify;` pattern. And, suppress a clippy lint about how `inotify::Reader::next` looks like `Iterator::next`. Add a comment explaining how it differs. And add a comment about what it does if there are no events waiting.
1 parent 3bf3410 commit e8bcc80

1 file changed

Lines changed: 52 additions & 0 deletions

File tree

src/fs/inotify.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,43 @@
11
//! inotify support for working with inotifies
2+
//!
3+
//! # Examples
4+
//!
5+
//! ```
6+
//! use rustix::fs::inotify;
7+
//! use rustix::io;
8+
//! use std::mem::MaybeUninit;
9+
//!
10+
//! # fn test() -> io::Result<()> {
11+
//! // Creeate an inotify object. In this example, we use `NONBLOCK` so that
12+
//! // the reader fails with `WOULDBLOCk` when no events are ready. Otherwise
13+
//! // it will block until at least one event is ready.
14+
//! let inotify = inotify::init(inotify::CreateFlags::NONBLOCK)?;
15+
//!
16+
//! // Add a directory to watch.
17+
//! inotify::add_watch(
18+
//! &inotify,
19+
//! "/path/to/some/directory/to/watch",
20+
//! inotify::WatchFlags::ALL_EVENTS,
21+
//! )?;
22+
//!
23+
//! // Generate some events in the watched directory...
24+
//!
25+
//! // Loop over pending events.
26+
//! let mut buf = [MaybeUninit::uninit(); 512];
27+
//! let mut iter = inotify::Reader::new(inotify, &mut buf);
28+
//! loop {
29+
//! let entry = match iter.next() {
30+
//! // Stop iterating if there are no more events for now.
31+
//! Err(io::Errno::WOULDBLOCK) => break,
32+
//! Err(e) => return Err(e),
33+
//! Ok(entry) => entry,
34+
//! };
35+
//!
36+
//! // Use `entry`...
37+
//! }
38+
//!
39+
//! # Ok(())
40+
//! # }
241
342
#![allow(unused_qualifications)]
443

@@ -130,7 +169,19 @@ impl<'a> InotifyEvent<'a> {
130169

131170
impl<'buf, Fd: AsFd> Reader<'buf, Fd> {
132171
/// Read the next inotify event.
172+
///
173+
/// This is similar to `[Iterator::next`] except that it doesn't return an
174+
/// `Option`, because the stream doesn't have an ending. It always returns
175+
/// events or errors.
176+
///
177+
/// If there are no events in the buffer and none ready to be read:
178+
/// - If the file descriptor was opened with
179+
/// [`inotify::CreateFlags::NONBLOCK`], this will fail with
180+
/// [`Errno::AGAIN`].
181+
/// - Otherwise this will block until at least one event is ready or an
182+
/// error occurs.
133183
#[allow(unsafe_code)]
184+
#[allow(clippy::should_implement_trait)]
134185
pub fn next(&mut self) -> io::Result<InotifyEvent> {
135186
if self.is_buffer_empty() {
136187
match read_uninit(self.fd.as_fd(), self.buf).map(|(init, _)| init.len()) {
@@ -144,6 +195,7 @@ impl<'buf, Fd: AsFd> Reader<'buf, Fd> {
144195
}
145196

146197
let ptr = self.buf[self.offset..].as_ptr();
198+
147199
// SAFETY:
148200
// - This data is initialized by the check above.
149201
// - Assumption: the kernel will not give us partial structs.

0 commit comments

Comments
 (0)