Skip to content

Commit 2c7ef82

Browse files
bors[bot]hellow554
andauthored
Merge #69
69: fix more clippy lints r=eldruin a=hellow554 Co-authored-by: Marcel Hellwig <[email protected]>
2 parents 0665e24 + d5cc3a8 commit 2c7ef82

File tree

2 files changed

+36
-39
lines changed

2 files changed

+36
-39
lines changed

src/errors.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ impl StdError for Error {
8888
}
8989

9090
impl From<IOError> for Error {
91-
fn from(err: IOError) -> Error {
92-
Error {
91+
fn from(err: IOError) -> Self {
92+
Self {
9393
kind: ErrorKind::Io(err),
9494
}
9595
}

src/lib.rs

+34-37
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,7 @@ pub use errors::*;
124124

125125
unsafe fn rstr_lcpy(dst: *mut libc::c_char, src: &str, length: usize) {
126126
let copylen = min(src.len() + 1, length);
127-
ptr::copy_nonoverlapping(
128-
src.as_bytes().as_ptr() as *const libc::c_char,
129-
dst,
130-
copylen - 1,
131-
);
127+
ptr::copy_nonoverlapping(src.as_bytes().as_ptr().cast(), dst, copylen - 1);
132128
slice::from_raw_parts_mut(dst, length)[copylen - 1] = 0;
133129
}
134130

@@ -209,12 +205,12 @@ pub fn chips() -> Result<ChipIterator> {
209205

210206
impl Chip {
211207
/// Open the GPIO Chip at the provided path (e.g. `/dev/gpiochip<N>`)
212-
pub fn new<P: AsRef<Path>>(path: P) -> Result<Chip> {
208+
pub fn new<P: AsRef<Path>>(path: P) -> Result<Self> {
213209
let f = File::open(path.as_ref())?;
214210
let mut info: ffi::gpiochip_info = unsafe { mem::zeroed() };
215211
ffi::gpio_get_chipinfo_ioctl(f.as_raw_fd(), &mut info)?;
216212

217-
Ok(Chip {
213+
Ok(Self {
218214
inner: Arc::new(InnerChip {
219215
file: f,
220216
path: path.as_ref().to_path_buf(),
@@ -406,11 +402,11 @@ unsafe fn cstrbuf_to_string(buf: &[libc::c_char]) -> Option<String> {
406402
}
407403

408404
impl Line {
409-
fn new(chip: Arc<InnerChip>, offset: u32) -> Result<Line> {
405+
fn new(chip: Arc<InnerChip>, offset: u32) -> Result<Self> {
410406
if offset >= chip.lines {
411407
return Err(offset_err(offset));
412408
}
413-
Ok(Line { chip, offset })
409+
Ok(Self { chip, offset })
414410
}
415411

416412
/// Get info about the line from the kernel.
@@ -491,8 +487,8 @@ impl Line {
491487
request.consumer_label[..].as_mut_ptr(),
492488
consumer,
493489
request.consumer_label.len(),
494-
)
495-
};
490+
);
491+
}
496492
ffi::gpio_get_linehandle_ioctl(self.chip.file.as_raw_fd(), &mut request)?;
497493
Ok(LineHandle {
498494
line: self.clone(),
@@ -557,8 +553,8 @@ impl Line {
557553
request.consumer_label[..].as_mut_ptr(),
558554
consumer,
559555
request.consumer_label.len(),
560-
)
561-
};
556+
);
557+
}
562558
ffi::gpio_get_lineevent_ioctl(self.chip.file.as_raw_fd(), &mut request)?;
563559

564560
Ok(LineEventHandle {
@@ -603,9 +599,10 @@ impl LineInfo {
603599
/// Lines are considered to be inputs if not explicitly
604600
/// marked as outputs in the line info flags by the kernel.
605601
pub fn direction(&self) -> LineDirection {
606-
match self.flags.contains(LineFlags::IS_OUT) {
607-
true => LineDirection::Out,
608-
false => LineDirection::In,
602+
if self.flags.contains(LineFlags::IS_OUT) {
603+
LineDirection::Out
604+
} else {
605+
LineDirection::In
609606
}
610607
}
611608

@@ -667,7 +664,7 @@ impl LineHandle {
667664
/// This value should be 0 or 1 which a "1" representing that
668665
/// the line is active. Usually this means that the line is
669666
/// at logic-level high but it could mean the opposite if the
670-
/// line has been marked as being ACTIVE_LOW.
667+
/// line has been marked as being `ACTIVE_LOW`.
671668
pub fn get_value(&self) -> Result<u8> {
672669
let mut data: ffi::gpiohandle_data = unsafe { mem::zeroed() };
673670
ffi::gpiohandle_get_line_values_ioctl(self.file.as_raw_fd(), &mut data)?;
@@ -678,7 +675,7 @@ impl LineHandle {
678675
///
679676
/// The value should be 0 or 1 with 1 representing a request
680677
/// to make the line "active". Usually "active" means
681-
/// logic level high unless the line has been marked as ACTIVE_LOW.
678+
/// logic level high unless the line has been marked as `ACTIVE_LOW`.
682679
///
683680
/// Calling `set_value` on a line that is not an output will
684681
/// likely result in an error (from the kernel).
@@ -701,7 +698,7 @@ impl LineHandle {
701698
}
702699

703700
impl AsRawFd for LineHandle {
704-
/// Gets the raw file descriptor for the LineHandle.
701+
/// Gets the raw file descriptor for the `LineHandle`.
705702
fn as_raw_fd(&self) -> RawFd {
706703
self.file.as_raw_fd()
707704
}
@@ -717,13 +714,13 @@ pub struct Lines {
717714
}
718715

719716
impl Lines {
720-
fn new(chip: Arc<InnerChip>, offsets: &[u32]) -> Result<Lines> {
717+
fn new(chip: Arc<InnerChip>, offsets: &[u32]) -> Result<Self> {
721718
let res: Result<Vec<Line>> = offsets
722719
.iter()
723720
.map(|off| Line::new(chip.clone(), *off))
724721
.collect();
725722
let lines = res?;
726-
Ok(Lines { lines })
723+
Ok(Self { lines })
727724
}
728725

729726
/// Get a handle to the parent chip for the lines
@@ -796,12 +793,12 @@ impl Lines {
796793
request.consumer_label[..].as_mut_ptr(),
797794
consumer,
798795
request.consumer_label.len(),
799-
)
800-
};
796+
);
797+
}
801798
ffi::gpio_get_linehandle_ioctl(self.lines[0].chip().inner.file.as_raw_fd(), &mut request)?;
802799
let lines = self.lines.clone();
803800
Ok(MultiLineHandle {
804-
lines: Lines { lines },
801+
lines: Self { lines },
805802
file: unsafe { File::from_raw_fd(request.fd) },
806803
})
807804
}
@@ -841,7 +838,7 @@ impl MultiLineHandle {
841838
/// This value should be 0 or 1 which a "1" representing that
842839
/// the line is active. Usually this means that the line is
843840
/// at logic-level high but it could mean the opposite if the
844-
/// line has been marked as being ACTIVE_LOW.
841+
/// line has been marked as being `ACTIVE_LOW`.
845842
pub fn get_values(&self) -> Result<Vec<u8>> {
846843
let mut data: ffi::gpiohandle_data = unsafe { mem::zeroed() };
847844
ffi::gpiohandle_get_line_values_ioctl(self.file.as_raw_fd(), &mut data)?;
@@ -854,7 +851,7 @@ impl MultiLineHandle {
854851
///
855852
/// The value should be 0 or 1 with 1 representing a request
856853
/// to make the line "active". Usually "active" means
857-
/// logic level high unless the line has been marked as ACTIVE_LOW.
854+
/// logic level high unless the line has been marked as `ACTIVE_LOW`.
858855
///
859856
/// Calling `set_value` on a line that is not an output will
860857
/// likely result in an error (from the kernel).
@@ -881,7 +878,7 @@ impl MultiLineHandle {
881878
}
882879

883880
impl AsRawFd for MultiLineHandle {
884-
/// Gets the raw file descriptor for the LineHandle.
881+
/// Gets the raw file descriptor for the `LineHandle`.
885882
fn as_raw_fd(&self) -> RawFd {
886883
self.file.as_raw_fd()
887884
}
@@ -923,8 +920,8 @@ impl LineEvent {
923920
/// in an interrupt handler so it should be very accurate.
924921
///
925922
/// The nanosecond timestamp value should are captured
926-
/// using the CLOCK_REALTIME offsets in the kernel and
927-
/// should be compared against CLOCK_REALTIME values.
923+
/// using the `CLOCK_REALTIME` offsets in the kernel and
924+
/// should be compared against `CLOCK_REALTIME` values.
928925
pub fn timestamp(&self) -> u64 {
929926
self.0.timestamp
930927
}
@@ -974,7 +971,7 @@ impl LineEventHandle {
974971
/// This value should be 0 or 1 which a "1" representing that
975972
/// the line is active. Usually this means that the line is
976973
/// at logic-level high but it could mean the opposite if the
977-
/// line has been marked as being ACTIVE_LOW.
974+
/// line has been marked as being `ACTIVE_LOW`.
978975
pub fn get_value(&self) -> Result<u8> {
979976
let mut data: ffi::gpiohandle_data = unsafe { mem::zeroed() };
980977
ffi::gpiohandle_get_line_values_ioctl(self.file.as_raw_fd(), &mut data)?;
@@ -990,23 +987,23 @@ impl LineEventHandle {
990987
/// enough data was read or the error returned by `read()`.
991988
pub(crate) fn read_event(&mut self) -> std::io::Result<Option<LineEvent>> {
992989
let mut data: ffi::gpioevent_data = unsafe { mem::zeroed() };
993-
let mut data_as_buf = unsafe {
990+
let data_as_buf = unsafe {
994991
slice::from_raw_parts_mut(
995-
&mut data as *mut ffi::gpioevent_data as *mut u8,
992+
(&mut data as *mut ffi::gpioevent_data).cast(),
996993
mem::size_of::<ffi::gpioevent_data>(),
997994
)
998995
};
999-
let bytes_read = self.file.read(&mut data_as_buf)?;
1000-
if bytes_read != mem::size_of::<ffi::gpioevent_data>() {
1001-
Ok(None)
1002-
} else {
996+
let bytes_read = self.file.read(data_as_buf)?;
997+
if bytes_read == mem::size_of::<ffi::gpioevent_data>() {
1003998
Ok(Some(LineEvent(data)))
999+
} else {
1000+
Ok(None)
10041001
}
10051002
}
10061003
}
10071004

10081005
impl AsRawFd for LineEventHandle {
1009-
/// Gets the raw file descriptor for the LineEventHandle.
1006+
/// Gets the raw file descriptor for the `LineEventHandle`.
10101007
fn as_raw_fd(&self) -> RawFd {
10111008
self.file.as_raw_fd()
10121009
}

0 commit comments

Comments
 (0)