diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index bff6689..4ab045b 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -1,40 +1,46 @@ name: Rust +env: + MSRV: "1.70" + on: [push, pull_request] jobs: - build: - + check: runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: dtolnay/rust-toolchain@beta + with: + components: clippy, rustfmt + - uses: Swatinem/rust-cache@v2 + - name: cargo fmt + run: cargo fmt --all --check + - name: clippy + run: cargo clippy --all-targets --all-features -- -D warnings + build: + runs-on: ubuntu-latest steps: - - uses: actions/checkout@v1 - - name: Install rust - uses: actions-rs/toolchain@v1 - with: - toolchain: 1.70.0 - override: true - components: clippy - - name: Check rust and cargo version - run: rustc -V && cargo -V - - name: Build - run: cargo build --verbose - - name: Run tests - run: cargo test --verbose - - name: Run Clippy lints - run: cargo clippy --verbose -- -D warnings - - name: Build (no default features) - run: cargo build --verbose --no-default-features - - name: Run tests (no default features) - run: cargo test --verbose --no-default-features - - name: Run Clippy lints (no default features) - run: cargo clippy --verbose --no-default-features -- -D warnings + - uses: actions/checkout@v4 + - uses: dtolnay/rust-toolchain@stable + - uses: Swatinem/rust-cache@v2 + - name: Check rustc and cargo version + run: rustc -V && cargo -V + - name: Build + run: cargo build --all-features --all-targets --verbose + - name: Run tests + run: cargo test --all-features --all-targets --verbose + - name: Build (no default features) + run: cargo build --no-default-features --all-targets --verbose + - name: Run tests (no default features) + run: cargo test --no-default-features --all-targets --verbose miri: name: "Miri" runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - name: Install Miri run: | rustup toolchain install nightly --component miri diff --git a/examples/stream.rs b/examples/stream.rs index ca2282f..60d7e94 100644 --- a/examples/stream.rs +++ b/examples/stream.rs @@ -1,31 +1,22 @@ -use std::{ - fs::File, - io, - thread, - time::Duration, -}; +use std::{fs::File, io, thread, time::Duration}; use futures_util::StreamExt; -use inotify::{ - Inotify, - WatchMask, -}; +use inotify::{Inotify, WatchMask}; use tempfile::TempDir; #[tokio::main] async fn main() -> Result<(), io::Error> { - let inotify = Inotify::init() - .expect("Failed to initialize inotify"); + let inotify = Inotify::init().expect("Failed to initialize inotify"); let dir = TempDir::new()?; - inotify.watches().add(dir.path(), WatchMask::CREATE | WatchMask::MODIFY)?; + inotify + .watches() + .add(dir.path(), WatchMask::CREATE | WatchMask::MODIFY)?; - thread::spawn::<_, Result<(), io::Error>>(move || { - loop { - File::create(dir.path().join("file"))?; - thread::sleep(Duration::from_millis(500)); - } + thread::spawn::<_, Result<(), io::Error>>(move || loop { + File::create(dir.path().join("file"))?; + thread::sleep(Duration::from_millis(500)); }); let mut buffer = [0; 1024]; diff --git a/examples/watch.rs b/examples/watch.rs index 8bdeb7b..6773659 100644 --- a/examples/watch.rs +++ b/examples/watch.rs @@ -1,18 +1,11 @@ use std::env; -use inotify::{ - EventMask, - Inotify, - WatchMask, -}; - +use inotify::{EventMask, Inotify, WatchMask}; fn main() { - let mut inotify = Inotify::init() - .expect("Failed to initialize inotify"); + let mut inotify = Inotify::init().expect("Failed to initialize inotify"); - let current_dir = env::current_dir() - .expect("Failed to determine current directory"); + let current_dir = env::current_dir().expect("Failed to determine current directory"); inotify .watches() diff --git a/src/events.rs b/src/events.rs index 88473f3..a3e2fc7 100644 --- a/src/events.rs +++ b/src/events.rs @@ -1,8 +1,5 @@ use std::{ - ffi::{ - OsStr, - OsString, - }, + ffi::{OsStr, OsString}, mem, os::unix::ffi::OsStrExt, sync::Weak, @@ -13,7 +10,6 @@ use inotify_sys as ffi; use crate::fd_guard::FdGuard; use crate::watches::WatchDescriptor; - /// Iterator over inotify events /// /// Allows for iteration over the events returned by @@ -23,16 +19,14 @@ use crate::watches::WatchDescriptor; /// [`Inotify::read_events`]: crate::Inotify::read_events #[derive(Debug)] pub struct Events<'a> { - fd : Weak, - buffer : &'a [u8], + fd: Weak, + buffer: &'a [u8], num_bytes: usize, - pos : usize, + pos: usize, } impl<'a> Events<'a> { - pub(crate) fn new(fd: Weak, buffer: &'a [u8], num_bytes: usize) - -> Self - { + pub(crate) fn new(fd: Weak, buffer: &'a [u8], num_bytes: usize) -> Self { Events { fd, buffer, @@ -51,14 +45,12 @@ impl<'a> Iterator for Events<'a> { self.pos += step; Some(event) - } - else { + } else { None } } } - /// An inotify event /// /// A file system event that describes a change that the user previously @@ -105,23 +97,13 @@ pub struct Event { } impl<'a> Event<&'a OsStr> { - fn new(fd: Weak, event: &ffi::inotify_event, name: &'a OsStr) - -> Self - { + fn new(fd: Weak, event: &ffi::inotify_event, name: &'a OsStr) -> Self { let mask = EventMask::from_bits(event.mask) .expect("Failed to convert event mask. This indicates a bug."); - let wd = crate::WatchDescriptor { - id: event.wd, - fd, - }; + let wd = crate::WatchDescriptor { id: event.wd, fd }; - let name = if name.is_empty() { - None - } - else { - Some(name) - }; + let name = if name.is_empty() { None } else { Some(name) }; Event { wd, @@ -141,12 +123,7 @@ impl<'a> Event<&'a OsStr> { /// # Panics /// /// Panics if the buffer does not contain a full event, including its name. - pub(crate) fn from_buffer( - fd : Weak, - buffer: &'a [u8], - ) - -> (usize, Self) - { + pub(crate) fn from_buffer(fd: Weak, buffer: &'a [u8]) -> (usize, Self) { let event_size = mem::size_of::(); // Make sure that the buffer is big enough to contain an event, without @@ -184,16 +161,9 @@ impl<'a> Event<&'a OsStr> { // // The `unwrap` here is safe, because `splitn` always returns at // least one result, even if the original slice contains no '\0'. - let name = name - .splitn(2, |b| b == &0u8) - .next() - .unwrap(); + let name = name.splitn(2, |b| b == &0u8).next().unwrap(); - let event = Event::new( - fd, - &ffi_event, - OsStr::from_bytes(name), - ); + let event = Event::new(fd, &ffi_event, OsStr::from_bytes(name)); (bytes_consumed, event) } @@ -217,11 +187,9 @@ impl<'a> Event<&'a OsStr> { } } - /// An owned version of `Event` pub type EventOwned = Event; - bitflags! { /// Indicates the type of an event /// @@ -370,50 +338,38 @@ impl EventMask { } } - #[cfg(test)] mod tests { - use std::{ - io::prelude::*, - mem, - slice, - sync, - }; + use std::{io::prelude::*, mem, slice, sync}; use inotify_sys as ffi; use super::Event; - #[test] fn from_buffer_should_not_mistake_next_event_for_name_of_previous_event() { let mut buffer = [0u8; 1024]; // First, put a normal event into the buffer let event = ffi::inotify_event { - wd: 0, - mask: 0, + wd: 0, + mask: 0, cookie: 0, - len: 0, // no name following after event + len: 0, // no name following after event }; let event = unsafe { - slice::from_raw_parts( - &event as *const _ as *const u8, - mem::size_of_val(&event), - ) + slice::from_raw_parts(&event as *const _ as *const u8, mem::size_of_val(&event)) }; - (&mut buffer[..]).write(event) + (&mut buffer[..]) + .write_all(event) .expect("Failed to write into buffer"); // After that event, simulate an event that starts with a non-zero byte. - buffer[mem::size_of_val(&event)] = 1; + buffer[mem::size_of_val(event)] = 1; // Now create the event and verify that the name is actually `None`, as // dictated by the value `len` above. - let (_, event) = Event::from_buffer( - sync::Weak::new(), - &buffer, - ); + let (_, event) = Event::from_buffer(sync::Weak::new(), &buffer); assert_eq!(event.name, None); } } diff --git a/src/fd_guard.rs b/src/fd_guard.rs index dfadfcd..ceeaad6 100644 --- a/src/fd_guard.rs +++ b/src/fd_guard.rs @@ -1,31 +1,19 @@ use std::{ ops::Deref, - os::unix::io::{ - AsFd, - AsRawFd, - BorrowedFd, - FromRawFd, - IntoRawFd, - RawFd, - }, - sync::atomic::{ - AtomicBool, - Ordering, - }, + os::unix::io::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, RawFd}, + sync::atomic::{AtomicBool, Ordering}, }; use inotify_sys as ffi; - /// A RAII guard around a `RawFd` that closes it automatically on drop. #[derive(Debug)] pub struct FdGuard { - pub(crate) fd : RawFd, + pub(crate) fd: RawFd, pub(crate) close_on_drop: AtomicBool, } impl FdGuard { - /// Indicate that the wrapped file descriptor should _not_ be closed /// when the guard is dropped. /// @@ -52,7 +40,9 @@ impl Deref for FdGuard { impl Drop for FdGuard { fn drop(&mut self) { if self.close_on_drop.load(Ordering::Acquire) { - unsafe { ffi::close(self.fd); } + unsafe { + ffi::close(self.fd); + } } } } diff --git a/src/inotify.rs b/src/inotify.rs index 0b1080f..640ae44 100644 --- a/src/inotify.rs +++ b/src/inotify.rs @@ -1,43 +1,21 @@ use std::{ io, - os::unix::io::{ - AsFd, - AsRawFd, - BorrowedFd, - FromRawFd, - IntoRawFd, - OwnedFd, - RawFd, - }, + os::unix::io::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, OwnedFd, RawFd}, path::Path, - sync::{ - atomic::AtomicBool, - Arc, - } + sync::{atomic::AtomicBool, Arc}, }; use inotify_sys as ffi; -use libc::{ - F_GETFL, - F_SETFL, - O_NONBLOCK, - fcntl, -}; +use libc::{fcntl, F_GETFL, F_SETFL, O_NONBLOCK}; use crate::events::Events; use crate::fd_guard::FdGuard; use crate::util::read_into_buffer; -use crate::watches::{ - WatchDescriptor, - WatchMask, - Watches, -}; - +use crate::watches::{WatchDescriptor, WatchMask, Watches}; #[cfg(feature = "stream")] use crate::stream::EventStream; - /// Idiomatic Rust wrapper around Linux's inotify API /// /// `Inotify` is a wrapper around an inotify instance. It generally tries to @@ -123,9 +101,9 @@ impl Inotify { /// Deprecated: use `Inotify.watches().add()` instead #[deprecated = "use `Inotify.watches().add()` instead"] - pub fn add_watch

(&mut self, path: P, mask: WatchMask) - -> io::Result - where P: AsRef + pub fn add_watch

(&mut self, path: P, mask: WatchMask) -> io::Result + where + P: AsRef, { self.watches().add(path, mask) } @@ -144,9 +122,7 @@ impl Inotify { /// This method calls [`Inotify::read_events`] internally and behaves /// essentially the same, apart from the blocking behavior. Please refer to /// the documentation of [`Inotify::read_events`] for more information. - pub fn read_events_blocking<'a>(&mut self, buffer: &'a mut [u8]) - -> io::Result> - { + pub fn read_events_blocking<'a>(&mut self, buffer: &'a mut [u8]) -> io::Result> { unsafe { let res = fcntl(**self.fd, F_GETFL); if res == -1 { @@ -177,8 +153,8 @@ impl Inotify { /// you need a method that will block until at least one event is available, /// please consider [`read_events_blocking`]. /// - /// Please note that inotify will merge identical successive unread events - /// into a single event. This means this method can not be used to count the + /// Please note that inotify will merge identical successive unread events + /// into a single event. This means this method can not be used to count the /// number of file system events. /// /// The `buffer` argument, as the name indicates, is used as a buffer for @@ -221,26 +197,23 @@ impl Inotify { /// [`read`]: libc::read /// [`ErrorKind::UnexpectedEof`]: std::io::ErrorKind::UnexpectedEof /// [`ErrorKind::InvalidInput`]: std::io::ErrorKind::InvalidInput - pub fn read_events<'a>(&mut self, buffer: &'a mut [u8]) - -> io::Result> - { + pub fn read_events<'a>(&mut self, buffer: &'a mut [u8]) -> io::Result> { let num_bytes = read_into_buffer(**self.fd, buffer); let num_bytes = match num_bytes { 0 => { - return Err( - io::Error::new( - io::ErrorKind::UnexpectedEof, - "`read` return `0`, signaling end-of-file" - ) - ); + return Err(io::Error::new( + io::ErrorKind::UnexpectedEof, + "`read` return `0`, signaling end-of-file", + )); } -1 => { let error = io::Error::last_os_error(); return Err(error); - }, + } _ if num_bytes < 0 => { - panic!("{} {} {} {} {} {}", + panic!( + "{} {} {} {} {} {}", "Unexpected return value from `read`. Received a negative", "value that was not `-1`. According to the `read` man page", "this shouldn't happen, as either `-1` is returned on", @@ -271,8 +244,7 @@ impl Inotify { /// as they will contend over one event source and each produce unpredictable stream contents. #[deprecated = "use `into_event_stream()` instead, which enforces a single Stream and predictable reads"] #[cfg(feature = "stream")] - pub fn event_stream(&mut self, buffer: T) - -> io::Result> + pub fn event_stream(&mut self, buffer: T) -> io::Result> where T: AsMut<[u8]> + AsRef<[u8]>, { @@ -286,8 +258,7 @@ impl Inotify { /// /// An internal buffer which can hold the largest possible event is used. #[cfg(feature = "stream")] - pub fn into_event_stream(self, buffer: T) - -> io::Result> + pub fn into_event_stream(self, buffer: T) -> io::Result> where T: AsMut<[u8]> + AsRef<[u8]>, { @@ -298,11 +269,8 @@ impl Inotify { /// initialized in `Inotify::init`. This is intended to be used to transform an /// `EventStream` back into an `Inotify`. Do not attempt to clone `Inotify` with this. #[cfg(feature = "stream")] - pub(crate) fn from_file_descriptor(fd: Arc) -> Self - { - Inotify { - fd, - } + pub(crate) fn from_file_descriptor(fd: Arc) -> Self { + Inotify { fd } } /// Closes the inotify instance @@ -353,7 +321,7 @@ impl AsRawFd for Inotify { impl FromRawFd for Inotify { unsafe fn from_raw_fd(fd: RawFd) -> Self { Inotify { - fd: Arc::new(FdGuard::from_raw_fd(fd)) + fd: Arc::new(FdGuard::from_raw_fd(fd)), } } } diff --git a/src/lib.rs b/src/lib.rs index e26e339..a55f81e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -65,12 +65,10 @@ //! [inotify]: https://en.wikipedia.org/wiki/Inotify //! [inotify man pages]: http://man7.org/linux/man-pages/man7/inotify.7.html - #![deny(missing_docs)] #![deny(warnings)] #![deny(missing_debug_implementations)] - #[macro_use] extern crate bitflags; @@ -83,23 +81,10 @@ mod watches; #[cfg(feature = "stream")] mod stream; - -pub use crate::events::{ - Event, - EventMask, - EventOwned, - Events, -}; +pub use crate::events::{Event, EventMask, EventOwned, Events}; pub use crate::inotify::Inotify; -pub use crate::util::{ - get_buffer_size, - get_absolute_path_buffer_size, -}; -pub use crate::watches::{ - Watches, - WatchDescriptor, - WatchMask, -}; +pub use crate::util::{get_absolute_path_buffer_size, get_buffer_size}; +pub use crate::watches::{WatchDescriptor, WatchMask, Watches}; #[cfg(feature = "stream")] pub use self::stream::EventStream; diff --git a/src/stream.rs b/src/stream.rs index 7bbf434..2922436 100644 --- a/src/stream.rs +++ b/src/stream.rs @@ -11,9 +11,9 @@ use tokio::io::unix::AsyncFd; use crate::events::{Event, EventOwned}; use crate::fd_guard::FdGuard; -use crate::Inotify; use crate::util::read_into_buffer; use crate::watches::Watches; +use crate::Inotify; /// Stream of inotify events /// diff --git a/src/watches.rs b/src/watches.rs index 2f0e66f..6f802cd 100644 --- a/src/watches.rs +++ b/src/watches.rs @@ -1,18 +1,12 @@ use std::{ cmp::Ordering, ffi::CString, - hash::{ - Hash, - Hasher, - }, + hash::{Hash, Hasher}, io, os::raw::c_int, os::unix::ffi::OsStrExt, path::Path, - sync::{ - Arc, - Weak, - }, + sync::{Arc, Weak}, }; use inotify_sys as ffi; @@ -254,9 +248,7 @@ pub struct Watches { impl Watches { /// Init watches with an inotify file descriptor pub(crate) fn new(fd: Arc) -> Self { - Watches { - fd, - } + Watches { fd } } /// Adds or updates a watch for the given path @@ -322,23 +314,21 @@ impl Watches { /// ``` /// /// [`inotify_add_watch`]: inotify_sys::inotify_add_watch - pub fn add

(&mut self, path: P, mask: WatchMask) - -> io::Result - where P: AsRef + pub fn add

(&mut self, path: P, mask: WatchMask) -> io::Result + where + P: AsRef, { let path = CString::new(path.as_ref().as_os_str().as_bytes())?; - let wd = unsafe { - ffi::inotify_add_watch( - **self.fd, - path.as_ptr() as *const _, - mask.bits(), - ) - }; + let wd = + unsafe { ffi::inotify_add_watch(**self.fd, path.as_ptr() as *const _, mask.bits()) }; match wd { -1 => Err(io::Error::last_os_error()), - _ => Ok(WatchDescriptor{ id: wd, fd: Arc::downgrade(&self.fd) }), + _ => Ok(WatchDescriptor { + id: wd, + fd: Arc::downgrade(&self.fd), + }), } } @@ -402,15 +392,13 @@ impl Watches { let result = unsafe { ffi::inotify_rm_watch(**self.fd, wd.id) }; match result { - 0 => Ok(()), + 0 => Ok(()), -1 => Err(io::Error::last_os_error()), - _ => panic!( - "unexpected return code from inotify_rm_watch ({})", result) + _ => panic!("unexpected return code from inotify_rm_watch ({})", result), } } } - /// Represents a watch on an inode /// /// Can be obtained from [`Watches::add`] or from an [`Event`]. A watch @@ -419,7 +407,7 @@ impl Watches { /// /// [`Event`]: crate::Event #[derive(Clone, Debug)] -pub struct WatchDescriptor{ +pub struct WatchDescriptor { pub(crate) id: c_int, pub(crate) fd: Weak, } @@ -428,7 +416,7 @@ impl Eq for WatchDescriptor {} impl PartialEq for WatchDescriptor { fn eq(&self, other: &Self) -> bool { - let self_fd = self.fd.upgrade(); + let self_fd = self.fd.upgrade(); let other_fd = other.fd.upgrade(); self.id == other.id && self_fd.is_some() && self_fd == other_fd diff --git a/tests/main.rs b/tests/main.rs index 4f9633c..2ad4486 100644 --- a/tests/main.rs +++ b/tests/main.rs @@ -1,37 +1,25 @@ #![deny(warnings)] - // This test suite is incomplete and doesn't cover all available functionality. // Contributions to improve test coverage would be highly appreciated! -use inotify::{ - Inotify, - WatchMask -}; +use inotify::{Inotify, WatchMask}; use std::fs::File; -use std::io::{ - Write, - ErrorKind, -}; -use std::os::unix::io::{ - AsRawFd, - FromRawFd, - IntoRawFd, -}; +use std::io::{ErrorKind, Write}; +use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd}; use std::path::PathBuf; use tempfile::TempDir; #[cfg(feature = "stream")] -use maplit::hashmap; +use futures_util::StreamExt; #[cfg(feature = "stream")] use inotify::EventMask; #[cfg(feature = "stream")] -use rand::{thread_rng, prelude::SliceRandom}; +use maplit::hashmap; #[cfg(feature = "stream")] -use std::sync::{Mutex, Arc}; +use rand::{prelude::SliceRandom, thread_rng}; #[cfg(feature = "stream")] -use futures_util::StreamExt; - +use std::sync::{Arc, Mutex}; #[test] fn it_should_watch_a_file() { @@ -84,11 +72,9 @@ async fn it_should_watch_a_file_async() { .await; let mut num_events = 0; - for event in events { - if let Ok(event) = event { - assert_eq!(watch, event.wd); - num_events += 1; - } + for event in events.into_iter().flatten() { + assert_eq!(watch, event.wd); + num_events += 1; } assert!(num_events > 0); } @@ -115,17 +101,12 @@ async fn it_should_watch_a_file_from_eventstream_watches() { let watch = watches.add(&path, WatchMask::MODIFY).unwrap(); write_to(&mut file); - let events = stream - .take(1) - .collect::>() - .await; + let events = stream.take(1).collect::>().await; let mut num_events = 0; - for event in events { - if let Ok(event) = event { - assert_eq!(watch, event.wd); - num_events += 1; - } + for event in events.into_iter().flatten() { + assert_eq!(watch, event.wd); + num_events += 1; } assert!(num_events > 0); } @@ -161,7 +142,10 @@ fn it_should_return_immediately_if_no_events_are_available() { let mut inotify = Inotify::init().unwrap(); let mut buffer = [0; 1024]; - assert_eq!(inotify.read_events(&mut buffer).unwrap_err().kind(), ErrorKind::WouldBlock); + assert_eq!( + inotify.read_events(&mut buffer).unwrap_err().kind(), + ErrorKind::WouldBlock + ); } #[test] @@ -170,7 +154,10 @@ fn it_should_convert_the_name_into_an_os_str() { let (path, mut file) = testdir.new_file(); let mut inotify = Inotify::init().unwrap(); - inotify.watches().add(&path.parent().unwrap(), WatchMask::MODIFY).unwrap(); + inotify + .watches() + .add(path.parent().unwrap(), WatchMask::MODIFY) + .unwrap(); write_to(&mut file); @@ -179,8 +166,7 @@ fn it_should_convert_the_name_into_an_os_str() { if let Some(event) = events.next() { assert_eq!(path.file_name(), event.name); - } - else { + } else { panic!("Expected inotify event"); } } @@ -200,8 +186,7 @@ fn it_should_set_name_to_none_if_it_is_empty() { if let Some(event) = events.next() { assert_eq!(event.name, None); - } - else { + } else { panic!("Expected inotify event"); } } @@ -215,9 +200,15 @@ fn it_should_not_accept_watchdescriptors_from_other_instances() { let _ = inotify.watches().add(&path, WatchMask::ACCESS).unwrap(); let second_inotify = Inotify::init().unwrap(); - let wd2 = second_inotify.watches().add(&path, WatchMask::ACCESS).unwrap(); + let wd2 = second_inotify + .watches() + .add(&path, WatchMask::ACCESS) + .unwrap(); - assert_eq!(inotify.watches().remove(wd2).unwrap_err().kind(), ErrorKind::InvalidInput); + assert_eq!( + inotify.watches().remove(wd2).unwrap_err().kind(), + ErrorKind::InvalidInput + ); } #[test] @@ -225,19 +216,11 @@ fn watch_descriptors_from_different_inotify_instances_should_not_be_equal() { let mut testdir = TestDir::new(); let (path, _) = testdir.new_file(); - let inotify_1 = Inotify::init() - .unwrap(); - let inotify_2 = Inotify::init() - .unwrap(); + let inotify_1 = Inotify::init().unwrap(); + let inotify_2 = Inotify::init().unwrap(); - let wd_1 = inotify_1 - .watches() - .add(&path, WatchMask::ACCESS) - .unwrap(); - let wd_2 = inotify_2 - .watches() - .add(&path, WatchMask::ACCESS) - .unwrap(); + let wd_1 = inotify_1.watches().add(&path, WatchMask::ACCESS).unwrap(); + let wd_2 = inotify_2.watches().add(&path, WatchMask::ACCESS).unwrap(); // As far as inotify is concerned, watch descriptors are just integers that // are scoped per inotify instance. This means that multiple instances will @@ -258,39 +241,27 @@ fn watch_descriptor_equality_should_not_be_confused_by_reused_fds() { // This is quite likely, but it doesn't happen every time. Therefore we may // need a few tries until we find two instances where that is the case. let (wd_1, inotify_2) = loop { - let inotify_1 = Inotify::init() - .unwrap(); + let inotify_1 = Inotify::init().unwrap(); - let wd_1 = inotify_1 - .watches() - .add(&path, WatchMask::ACCESS) - .unwrap(); + let wd_1 = inotify_1.watches().add(&path, WatchMask::ACCESS).unwrap(); let fd_1 = inotify_1.as_raw_fd(); - inotify_1 - .close() - .unwrap(); - let inotify_2 = Inotify::init() - .unwrap(); + inotify_1.close().unwrap(); + let inotify_2 = Inotify::init().unwrap(); if fd_1 == inotify_2.as_raw_fd() { break (wd_1, inotify_2); } }; - let wd_2 = inotify_2 - .watches() - .add(&path, WatchMask::ACCESS) - .unwrap(); + let wd_2 = inotify_2.watches().add(&path, WatchMask::ACCESS).unwrap(); // The way we engineered this situation, both `WatchDescriptor` instances // have the same fields. They still come from different inotify instances // though, so they shouldn't be equal. assert!(wd_1 != wd_2); - inotify_2 - .close() - .unwrap(); + inotify_2.close().unwrap(); // A little extra gotcha: If both inotify instances are closed, and the `Eq` // implementation naively compares the weak pointers, both will be `None`, @@ -366,8 +337,14 @@ async fn it_should_distinguish_event_for_files_with_same_name() { let (path_2, _) = testdir.new_file_in_directory_with_name("another_dir", "file_a"); // watching both files for `DELETE_SELF` - let wd_1 = inotify.watches().add(&path_1, WatchMask::DELETE_SELF).unwrap(); - let wd_2 = inotify.watches().add(&path_2, WatchMask::DELETE_SELF).unwrap(); + let wd_1 = inotify + .watches() + .add(&path_1, WatchMask::DELETE_SELF) + .unwrap(); + let wd_2 = inotify + .watches() + .add(&path_2, WatchMask::DELETE_SELF) + .unwrap(); let expected_ids = hashmap! { wd_1.get_watch_descriptor_id() => "file_a", @@ -467,9 +444,6 @@ impl TestDir { } fn write_to(file: &mut File) { - file - .write(b"This should trigger an inotify event.") - .unwrap_or_else(|error| - panic!("Failed to write to file: {}", error) - ); + file.write_all(b"This should trigger an inotify event.") + .unwrap_or_else(|error| panic!("Failed to write to file: {}", error)); }