Skip to content

Commit fe4c0c1

Browse files
committed
Fix clippy errors in sample code
The original code masks flags with O_RDONLY, which is 0: ``` .read((flags & O_RDONLY != 0) | (flags & O_RDWR != 0)) .write((flags & O_WRONLY != 0) | (flags & O_RDWR != 0)) ``` This generates Clippy errors because it's equivalent to 0 & 0 != 0. The correct code uses O_ACCMODE to mask off the lower bits: ``` .read((flags & O_ACCMODE == O_RDONLY) | (flags & O_ACCMODE == O_RDWR)) .write((flags & O_ACCMODE == O_WRONLY) | (flags & O_ACCMODE == O_RDWR)) ``` The code could be further simplified with `let access_flags = flags & O_ACCMODE` at the top, but this version maintains the original intent and consistency with similar libinput examples across the internet. This commit updates all 3 occurrences of the code fragment. Fixes Smithay#66 Signed-off-by: Tom Stokes <[email protected]>
1 parent 40b31d3 commit fe4c0c1

File tree

3 files changed

+9
-9
lines changed

3 files changed

+9
-9
lines changed

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ Configure and run event loop:
3535

3636
```rust
3737
use input::{Libinput, LibinputInterface};
38-
use libc::{O_RDONLY, O_RDWR, O_WRONLY};
38+
use libc::{O_ACCMODE, O_RDONLY, O_RDWR, O_WRONLY};
3939
use std::fs::{File, OpenOptions};
4040
use std::os::unix::{fs::OpenOptionsExt, io::OwnedFd};
4141
use std::path::Path;
@@ -46,8 +46,8 @@ impl LibinputInterface for Interface {
4646
fn open_restricted(&mut self, path: &Path, flags: i32) -> Result<OwnedFd, i32> {
4747
OpenOptions::new()
4848
.custom_flags(flags)
49-
.read((flags & O_RDONLY != 0) | (flags & O_RDWR != 0))
50-
.write((flags & O_WRONLY != 0) | (flags & O_RDWR != 0))
49+
.read((flags & O_ACCMODE == O_RDONLY) | (flags & O_ACCMODE == O_RDWR))
50+
.write((flags & O_ACCMODE == O_WRONLY) | (flags & O_ACCMODE == O_RDWR))
5151
.open(path)
5252
.map(|file| file.into())
5353
.map_err(|err| err.raw_os_error().unwrap())

src/context.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ impl Libinput {
351351
/// # use std::fs::{File, OpenOptions};
352352
/// # use std::os::unix::{fs::OpenOptionsExt, io::OwnedFd};
353353
/// # use std::path::Path;
354-
/// # use libc::{O_RDONLY, O_RDWR, O_WRONLY};
354+
/// # use libc::{O_ACCMODE, O_RDONLY, O_RDWR, O_WRONLY};
355355
/// #
356356
/// use input::{Libinput, LibinputInterface};
357357
/// use rustix::event::{poll, PollFlags, PollFd};
@@ -362,8 +362,8 @@ impl Libinput {
362362
/// # fn open_restricted(&mut self, path: &Path, flags: i32) -> Result<OwnedFd, i32> {
363363
/// # OpenOptions::new()
364364
/// # .custom_flags(flags)
365-
/// # .read((flags & O_RDONLY != 0) | (flags & O_RDWR != 0))
366-
/// # .write((flags & O_WRONLY != 0) | (flags & O_RDWR != 0))
365+
/// # .read((flags & O_ACCMODE == O_RDONLY) | (flags & O_ACCMODE == O_RDWR))
366+
/// # .write((flags & O_ACCMODE == O_WRONLY) | (flags & O_ACCMODE == O_RDWR))
367367
/// # .open(path)
368368
/// # .map(|file| file.into())
369369
/// # .map_err(|err| err.raw_os_error().unwrap())

src/lib.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,16 @@
3939
//! use std::path::Path;
4040
//!
4141
//! extern crate libc;
42-
//! use libc::{O_RDONLY, O_RDWR, O_WRONLY};
42+
//! use libc::{O_ACCMODE, O_RDONLY, O_RDWR, O_WRONLY};
4343
//!
4444
//! struct Interface;
4545
//!
4646
//! impl LibinputInterface for Interface {
4747
//! fn open_restricted(&mut self, path: &Path, flags: i32) -> Result<OwnedFd, i32> {
4848
//! OpenOptions::new()
4949
//! .custom_flags(flags)
50-
//! .read((flags & O_RDONLY != 0) | (flags & O_RDWR != 0))
51-
//! .write((flags & O_WRONLY != 0) | (flags & O_RDWR != 0))
50+
//! .read((flags & O_ACCMODE == O_RDONLY) | (flags & O_ACCMODE == O_RDWR))
51+
//! .write((flags & O_ACCMODE == O_WRONLY) | (flags & O_ACCMODE == O_RDWR))
5252
//! .open(path)
5353
//! .map(|file| file.into())
5454
//! .map_err(|err| err.raw_os_error().unwrap())

0 commit comments

Comments
 (0)