Skip to content

Commit b4ef60b

Browse files
phip1611eldruin
authored andcommitted
applied rustfmt
1 parent 9f81f40 commit b4ef60b

File tree

2 files changed

+57
-32
lines changed

2 files changed

+57
-32
lines changed

src/lib.rs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,11 @@ pub mod spidevioctl;
6969
pub use crate::spidevioctl::SpidevTransfer;
7070

7171
use bitflags::bitflags;
72+
use std::fs::{File, OpenOptions};
7273
use std::io;
7374
use std::io::prelude::*;
74-
use std::fs::{File, OpenOptions};
75-
use std::path::Path;
7675
use std::os::unix::prelude::*;
76+
use std::path::Path;
7777

7878
// Constants extracted from linux/spi/spidev.h
7979
bitflags! {
@@ -203,18 +203,17 @@ impl Spidev {
203203
Self { devfile }
204204
}
205205

206-
207206
/// Open the spidev device with the provided path
208207
///
209208
/// Typically, the path will be something like `"/dev/spidev0.0"`
210209
/// where the first number if the bus and the second number
211210
/// is the chip select on that bus for the device being targeted.
212211
pub fn open<P: AsRef<Path>>(path: P) -> io::Result<Spidev> {
213212
let devfile = OpenOptions::new()
214-
.read(true)
215-
.write(true)
216-
.create(false)
217-
.open(path)?;
213+
.read(true)
214+
.write(true)
215+
.create(false)
216+
.open(path)?;
218217
Ok(Self::new(devfile))
219218
}
220219

@@ -282,16 +281,16 @@ impl Write for Spidev {
282281

283282
#[cfg(test)]
284283
mod test {
285-
use super::{SpidevOptions, SpiModeFlags};
284+
use super::{SpiModeFlags, SpidevOptions};
286285

287286
#[test]
288287
fn test_spidev_options_all() {
289288
let options = SpidevOptions::new()
290-
.bits_per_word(8)
291-
.max_speed_hz(20_000)
292-
.lsb_first(false)
293-
.mode(SpiModeFlags::SPI_MODE_0)
294-
.build();
289+
.bits_per_word(8)
290+
.max_speed_hz(20_000)
291+
.lsb_first(false)
292+
.mode(SpiModeFlags::SPI_MODE_0)
293+
.build();
295294
assert_eq!(options.bits_per_word, Some(8));
296295
assert_eq!(options.max_speed_hz, Some(20_000));
297296
assert_eq!(options.lsb_first, Some(false));

src/spidevioctl.rs

Lines changed: 45 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,17 @@
77
// except according to those terms.
88

99
// macros import
10-
use nix::{ioctl_read, ioctl_write_ptr, ioctl_write_buf};
10+
use super::SpiModeFlags;
11+
use nix::{ioctl_read, ioctl_write_buf, ioctl_write_ptr};
1112
use std::io;
1213
use std::marker::PhantomData;
1314
use std::os::unix::prelude::*;
14-
use super::SpiModeFlags;
1515

1616
fn from_nix_error(err: ::nix::Error) -> io::Error {
17-
io::Error::from_raw_os_error(err.as_errno().unwrap_or_else(|| nix::errno::Errno::UnknownErrno) as i32)
17+
io::Error::from_raw_os_error(
18+
err.as_errno()
19+
.unwrap_or_else(|| nix::errno::Errno::UnknownErrno) as i32,
20+
)
1821
}
1922

2023
fn from_nix_result<T>(res: ::nix::Result<T>) -> io::Result<T> {
@@ -24,7 +27,6 @@ fn from_nix_result<T>(res: ::nix::Result<T>) -> io::Result<T> {
2427
}
2528
}
2629

27-
2830
/// Structure that is used when performing communication
2931
/// with the kernel.
3032
///
@@ -130,18 +132,48 @@ mod ioctl {
130132
ioctl_read!(get_lsb_first, SPI_IOC_MAGIC, SPI_IOC_NR_LSB_FIRST, u8);
131133
ioctl_write_ptr!(set_lsb_first, SPI_IOC_MAGIC, SPI_IOC_NR_LSB_FIRST, u8);
132134

133-
ioctl_read!(get_bits_per_word, SPI_IOC_MAGIC, SPI_IOC_NR_BITS_PER_WORD, u8);
134-
ioctl_write_ptr!(set_bits_per_word, SPI_IOC_MAGIC, SPI_IOC_NR_BITS_PER_WORD, u8);
135-
136-
ioctl_read!(get_max_speed_hz, SPI_IOC_MAGIC, SPI_IOC_NR_MAX_SPEED_HZ, u32);
137-
ioctl_write_ptr!(set_max_speed_hz, SPI_IOC_MAGIC, SPI_IOC_NR_MAX_SPEED_HZ, u32);
135+
ioctl_read!(
136+
get_bits_per_word,
137+
SPI_IOC_MAGIC,
138+
SPI_IOC_NR_BITS_PER_WORD,
139+
u8
140+
);
141+
ioctl_write_ptr!(
142+
set_bits_per_word,
143+
SPI_IOC_MAGIC,
144+
SPI_IOC_NR_BITS_PER_WORD,
145+
u8
146+
);
147+
148+
ioctl_read!(
149+
get_max_speed_hz,
150+
SPI_IOC_MAGIC,
151+
SPI_IOC_NR_MAX_SPEED_HZ,
152+
u32
153+
);
154+
ioctl_write_ptr!(
155+
set_max_speed_hz,
156+
SPI_IOC_MAGIC,
157+
SPI_IOC_NR_MAX_SPEED_HZ,
158+
u32
159+
);
138160

139161
// NOTE: this macro works for single transfers but cannot properly
140162
// calculate size for multi transfer whose length we will not know
141163
// until runtime. We fallback to using the underlying ioctl for that
142164
// use case.
143-
ioctl_write_ptr!(spidev_transfer, SPI_IOC_MAGIC, SPI_IOC_NR_TRANSFER, spi_ioc_transfer);
144-
ioctl_write_buf!(spidev_transfer_buf, SPI_IOC_MAGIC, SPI_IOC_NR_TRANSFER, spi_ioc_transfer);
165+
ioctl_write_ptr!(
166+
spidev_transfer,
167+
SPI_IOC_MAGIC,
168+
SPI_IOC_NR_TRANSFER,
169+
spi_ioc_transfer
170+
);
171+
ioctl_write_buf!(
172+
spidev_transfer_buf,
173+
SPI_IOC_MAGIC,
174+
SPI_IOC_NR_TRANSFER,
175+
spi_ioc_transfer
176+
);
145177
}
146178

147179
/// Representation of a spidev transfer that is shared
@@ -175,11 +207,7 @@ pub fn get_lsb_first(fd: RawFd) -> io::Result<u8> {
175207
}
176208

177209
pub fn set_lsb_first(fd: RawFd, lsb_first: bool) -> io::Result<()> {
178-
let lsb_first_value: u8 = if lsb_first {
179-
1
180-
} else {
181-
0
182-
};
210+
let lsb_first_value: u8 = if lsb_first { 1 } else { 0 };
183211
from_nix_result(unsafe { ioctl::set_lsb_first(fd, &lsb_first_value) })?;
184212
Ok(())
185213
}
@@ -214,8 +242,6 @@ pub fn transfer(fd: RawFd, transfer: &mut SpidevTransfer) -> io::Result<()> {
214242
}
215243

216244
pub fn transfer_multiple(fd: RawFd, transfers: &mut [SpidevTransfer]) -> io::Result<()> {
217-
from_nix_result(unsafe {
218-
ioctl::spidev_transfer_buf(fd, transfers)
219-
})?;
245+
from_nix_result(unsafe { ioctl::spidev_transfer_buf(fd, transfers) })?;
220246
Ok(())
221247
}

0 commit comments

Comments
 (0)