Skip to content

Commit 8ecd7bc

Browse files
Merge #1935
1935: Formatting only changes for #1928 and #1863 r=asomers a=SUPERCILEX Co-authored-by: Alex Saveau <[email protected]>
2 parents 3d3e6b9 + c5cc9dd commit 8ecd7bc

File tree

3 files changed

+1026
-435
lines changed

3 files changed

+1026
-435
lines changed

src/fcntl.rs

Lines changed: 91 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -193,9 +193,13 @@ feature! {
193193

194194
// The conversion is not identical on all operating systems.
195195
#[allow(clippy::useless_conversion)]
196-
pub fn open<P: ?Sized + NixPath>(path: &P, oflag: OFlag, mode: Mode) -> Result<RawFd> {
197-
let fd = path.with_nix_path(|cstr| {
198-
unsafe { libc::open(cstr.as_ptr(), oflag.bits(), mode.bits() as c_uint) }
196+
pub fn open<P: ?Sized + NixPath>(
197+
path: &P,
198+
oflag: OFlag,
199+
mode: Mode,
200+
) -> Result<RawFd> {
201+
let fd = path.with_nix_path(|cstr| unsafe {
202+
libc::open(cstr.as_ptr(), oflag.bits(), mode.bits() as c_uint)
199203
})?;
200204

201205
Errno::result(fd)
@@ -210,8 +214,8 @@ pub fn openat<P: ?Sized + NixPath>(
210214
oflag: OFlag,
211215
mode: Mode,
212216
) -> Result<RawFd> {
213-
let fd = path.with_nix_path(|cstr| {
214-
unsafe { libc::openat(dirfd, cstr.as_ptr(), oflag.bits(), mode.bits() as c_uint) }
217+
let fd = path.with_nix_path(|cstr| unsafe {
218+
libc::openat(dirfd, cstr.as_ptr(), oflag.bits(), mode.bits() as c_uint)
215219
})?;
216220
Errno::result(fd)
217221
}
@@ -303,7 +307,10 @@ fn readlink_maybe_at<P: ?Sized + NixPath>(
303307
})
304308
}
305309

306-
fn inner_readlink<P: ?Sized + NixPath>(dirfd: Option<RawFd>, path: &P) -> Result<OsString> {
310+
fn inner_readlink<P: ?Sized + NixPath>(
311+
dirfd: Option<RawFd>,
312+
path: &P,
313+
) -> Result<OsString> {
307314
let mut v = Vec::with_capacity(libc::PATH_MAX as usize);
308315

309316
{
@@ -387,7 +394,10 @@ pub fn readlink<P: ?Sized + NixPath>(path: &P) -> Result<OsString> {
387394
}
388395

389396
#[cfg(not(target_os = "redox"))]
390-
pub fn readlinkat<P: ?Sized + NixPath>(dirfd: RawFd, path: &P) -> Result<OsString> {
397+
pub fn readlinkat<P: ?Sized + NixPath>(
398+
dirfd: RawFd,
399+
path: &P,
400+
) -> Result<OsString> {
391401
inner_readlink(Some(dirfd), path)
392402
}
393403

@@ -450,9 +460,17 @@ pub enum FcntlArg<'a> {
450460
F_OFD_SETLKW(&'a libc::flock),
451461
#[cfg(any(target_os = "linux", target_os = "android"))]
452462
F_OFD_GETLK(&'a mut libc::flock),
453-
#[cfg(any(target_os = "android", target_os = "linux", target_os = "freebsd"))]
463+
#[cfg(any(
464+
target_os = "android",
465+
target_os = "linux",
466+
target_os = "freebsd"
467+
))]
454468
F_ADD_SEALS(SealFlag),
455-
#[cfg(any(target_os = "android", target_os = "linux", target_os = "freebsd"))]
469+
#[cfg(any(
470+
target_os = "android",
471+
target_os = "linux",
472+
target_os = "freebsd"
473+
))]
456474
F_GET_SEALS,
457475
#[cfg(any(target_os = "macos", target_os = "ios"))]
458476
F_FULLFSYNC,
@@ -481,7 +499,9 @@ pub fn fcntl(fd: RawFd, arg: FcntlArg) -> Result<c_int> {
481499
let res = unsafe {
482500
match arg {
483501
F_DUPFD(rawfd) => libc::fcntl(fd, libc::F_DUPFD, rawfd),
484-
F_DUPFD_CLOEXEC(rawfd) => libc::fcntl(fd, libc::F_DUPFD_CLOEXEC, rawfd),
502+
F_DUPFD_CLOEXEC(rawfd) => {
503+
libc::fcntl(fd, libc::F_DUPFD_CLOEXEC, rawfd)
504+
}
485505
F_GETFD => libc::fcntl(fd, libc::F_GETFD),
486506
F_SETFD(flag) => libc::fcntl(fd, libc::F_SETFD, flag.bits()),
487507
F_GETFL => libc::fcntl(fd, libc::F_GETFL),
@@ -498,9 +518,19 @@ pub fn fcntl(fd: RawFd, arg: FcntlArg) -> Result<c_int> {
498518
F_OFD_SETLKW(flock) => libc::fcntl(fd, libc::F_OFD_SETLKW, flock),
499519
#[cfg(any(target_os = "android", target_os = "linux"))]
500520
F_OFD_GETLK(flock) => libc::fcntl(fd, libc::F_OFD_GETLK, flock),
501-
#[cfg(any(target_os = "android", target_os = "linux", target_os = "freebsd"))]
502-
F_ADD_SEALS(flag) => libc::fcntl(fd, libc::F_ADD_SEALS, flag.bits()),
503-
#[cfg(any(target_os = "android", target_os = "linux", target_os = "freebsd"))]
521+
#[cfg(any(
522+
target_os = "android",
523+
target_os = "linux",
524+
target_os = "freebsd"
525+
))]
526+
F_ADD_SEALS(flag) => {
527+
libc::fcntl(fd, libc::F_ADD_SEALS, flag.bits())
528+
}
529+
#[cfg(any(
530+
target_os = "android",
531+
target_os = "linux",
532+
target_os = "freebsd"
533+
))]
504534
F_GET_SEALS => libc::fcntl(fd, libc::F_GET_SEALS),
505535
#[cfg(any(target_os = "macos", target_os = "ios"))]
506536
F_FULLFSYNC => libc::fcntl(fd, libc::F_FULLFSYNC),
@@ -535,8 +565,12 @@ pub fn flock(fd: RawFd, arg: FlockArg) -> Result<()> {
535565
LockShared => libc::flock(fd, libc::LOCK_SH),
536566
LockExclusive => libc::flock(fd, libc::LOCK_EX),
537567
Unlock => libc::flock(fd, libc::LOCK_UN),
538-
LockSharedNonblock => libc::flock(fd, libc::LOCK_SH | libc::LOCK_NB),
539-
LockExclusiveNonblock => libc::flock(fd, libc::LOCK_EX | libc::LOCK_NB),
568+
LockSharedNonblock => {
569+
libc::flock(fd, libc::LOCK_SH | libc::LOCK_NB)
570+
}
571+
LockExclusiveNonblock => {
572+
libc::flock(fd, libc::LOCK_EX | libc::LOCK_NB)
573+
}
540574
UnlockNonblock => libc::flock(fd, libc::LOCK_UN | libc::LOCK_NB),
541575
}
542576
};
@@ -632,12 +666,19 @@ pub fn splice(
632666
.map(|offset| offset as *mut libc::loff_t)
633667
.unwrap_or(ptr::null_mut());
634668

635-
let ret = unsafe { libc::splice(fd_in, off_in, fd_out, off_out, len, flags.bits()) };
669+
let ret = unsafe {
670+
libc::splice(fd_in, off_in, fd_out, off_out, len, flags.bits())
671+
};
636672
Errno::result(ret).map(|r| r as usize)
637673
}
638674

639675
#[cfg(any(target_os = "linux", target_os = "android"))]
640-
pub fn tee(fd_in: RawFd, fd_out: RawFd, len: usize, flags: SpliceFFlags) -> Result<usize> {
676+
pub fn tee(
677+
fd_in: RawFd,
678+
fd_out: RawFd,
679+
len: usize,
680+
flags: SpliceFFlags,
681+
) -> Result<usize> {
641682
let ret = unsafe { libc::tee(fd_in, fd_out, len, flags.bits()) };
642683
Errno::result(ret).map(|r| r as usize)
643684
}
@@ -646,9 +687,8 @@ pub fn tee(fd_in: RawFd, fd_out: RawFd, len: usize, flags: SpliceFFlags) -> Resu
646687
pub fn vmsplice(
647688
fd: RawFd,
648689
iov: &[std::io::IoSlice<'_>],
649-
flags: SpliceFFlags
650-
) -> Result<usize>
651-
{
690+
flags: SpliceFFlags,
691+
) -> Result<usize> {
652692
let ret = unsafe {
653693
libc::vmsplice(
654694
fd,
@@ -778,14 +818,19 @@ impl SpacectlRange {
778818
/// ```
779819
#[cfg(target_os = "freebsd")]
780820
pub fn fspacectl(fd: RawFd, range: SpacectlRange) -> Result<SpacectlRange> {
781-
let mut rqsr = libc::spacectl_range{r_offset: range.0, r_len: range.1};
782-
let res = unsafe { libc::fspacectl(
821+
let mut rqsr = libc::spacectl_range {
822+
r_offset: range.0,
823+
r_len: range.1,
824+
};
825+
let res = unsafe {
826+
libc::fspacectl(
783827
fd,
784828
libc::SPACECTL_DEALLOC, // Only one command is supported ATM
785829
&rqsr,
786-
0, // No flags are currently supported
787-
&mut rqsr
788-
)};
830+
0, // No flags are currently supported
831+
&mut rqsr,
832+
)
833+
};
789834
Errno::result(res).map(|_| SpacectlRange(rqsr.r_offset, rqsr.r_len))
790835
}
791836

@@ -820,18 +865,25 @@ pub fn fspacectl(fd: RawFd, range: SpacectlRange) -> Result<SpacectlRange> {
820865
/// assert_eq!(buf, b"012\0\0\0\0\0\09abcdef");
821866
/// ```
822867
#[cfg(target_os = "freebsd")]
823-
pub fn fspacectl_all(fd: RawFd, offset: libc::off_t, len: libc::off_t)
824-
-> Result<()>
825-
{
826-
let mut rqsr = libc::spacectl_range{r_offset: offset, r_len: len};
868+
pub fn fspacectl_all(
869+
fd: RawFd,
870+
offset: libc::off_t,
871+
len: libc::off_t,
872+
) -> Result<()> {
873+
let mut rqsr = libc::spacectl_range {
874+
r_offset: offset,
875+
r_len: len,
876+
};
827877
while rqsr.r_len > 0 {
828-
let res = unsafe { libc::fspacectl(
878+
let res = unsafe {
879+
libc::fspacectl(
829880
fd,
830881
libc::SPACECTL_DEALLOC, // Only one command is supported ATM
831882
&rqsr,
832-
0, // No flags are currently supported
833-
&mut rqsr
834-
)};
883+
0, // No flags are currently supported
884+
&mut rqsr,
885+
)
886+
};
835887
Errno::result(res)?;
836888
}
837889
Ok(())
@@ -848,8 +900,8 @@ pub fn fspacectl_all(fd: RawFd, offset: libc::off_t, len: libc::off_t)
848900
))]
849901
mod posix_fadvise {
850902
use crate::errno::Errno;
851-
use std::os::unix::io::RawFd;
852903
use crate::Result;
904+
use std::os::unix::io::RawFd;
853905

854906
#[cfg(feature = "fs")]
855907
libc_enum! {
@@ -894,7 +946,11 @@ mod posix_fadvise {
894946
target_os = "wasi",
895947
target_os = "freebsd"
896948
))]
897-
pub fn posix_fallocate(fd: RawFd, offset: libc::off_t, len: libc::off_t) -> Result<()> {
949+
pub fn posix_fallocate(
950+
fd: RawFd,
951+
offset: libc::off_t,
952+
len: libc::off_t,
953+
) -> Result<()> {
898954
let res = unsafe { libc::posix_fallocate(fd, offset, len) };
899955
match Errno::result(res) {
900956
Err(err) => Err(err),

src/sys/epoll.rs

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::errno::Errno;
22
use crate::Result;
33
use libc::{self, c_int};
44
use std::mem;
5-
use std::os::unix::io::{FromRawFd,RawFd, OwnedFd, AsFd, AsRawFd};
5+
use std::os::unix::io::{AsFd, AsRawFd, FromRawFd, OwnedFd, RawFd};
66

77
libc_bitflags!(
88
pub struct EpollFlags: c_int {
@@ -78,22 +78,22 @@ impl EpollEvent {
7878
/// # fn main() -> nix::Result<()> {
7979
/// const DATA: u64 = 17;
8080
/// const MILLIS: u64 = 100;
81-
///
81+
///
8282
/// // Create epoll
8383
/// let epoll = Epoll::new(EpollCreateFlags::empty())?;
84-
///
84+
///
8585
/// // Create eventfd & Add event
8686
/// let eventfd = unsafe { OwnedFd::from_raw_fd(eventfd(0, EfdFlags::empty())?) };
8787
/// epoll.add(&eventfd, EpollEvent::new(EpollFlags::EPOLLIN,DATA))?;
88-
///
88+
///
8989
/// // Arm eventfd & Time wait
9090
/// write(eventfd.as_raw_fd(), &1u64.to_ne_bytes())?;
9191
/// let now = Instant::now();
92-
///
92+
///
9393
/// // Wait on event
9494
/// let mut events = [EpollEvent::empty()];
9595
/// epoll.wait(&mut events, MILLIS as isize)?;
96-
///
96+
///
9797
/// // Assert data correct & timeout didn't occur
9898
/// assert_eq!(events[0].data(), DATA);
9999
/// assert!(now.elapsed() < Duration::from_millis(MILLIS));
@@ -104,7 +104,7 @@ impl EpollEvent {
104104
pub struct Epoll(pub OwnedFd);
105105
impl Epoll {
106106
/// Creates a new epoll instance and returns a file descriptor referring to that instance.
107-
///
107+
///
108108
/// [`epoll_create1`](https://man7.org/linux/man-pages/man2/epoll_create1.2.html).
109109
pub fn new(flags: EpollCreateFlags) -> Result<Self> {
110110
let res = unsafe { libc::epoll_create1(flags.bits()) };
@@ -113,30 +113,38 @@ impl Epoll {
113113
Ok(Self(owned_fd))
114114
}
115115
/// Add an entry to the interest list of the epoll file descriptor for
116-
/// specified in events.
117-
///
116+
/// specified in events.
117+
///
118118
/// [`epoll_ctl`](https://man7.org/linux/man-pages/man2/epoll_ctl.2.html) with `EPOLL_CTL_ADD`.
119119
pub fn add<Fd: AsFd>(&self, fd: Fd, mut event: EpollEvent) -> Result<()> {
120-
self.epoll_ctl(EpollOp::EpollCtlAdd,fd,&mut event)
120+
self.epoll_ctl(EpollOp::EpollCtlAdd, fd, &mut event)
121121
}
122122
/// Remove (deregister) the target file descriptor `fd` from the interest list.
123-
///
123+
///
124124
/// [`epoll_ctl`](https://man7.org/linux/man-pages/man2/epoll_ctl.2.html) with `EPOLL_CTL_DEL` .
125125
pub fn delete<Fd: AsFd>(&self, fd: Fd) -> Result<()> {
126-
self.epoll_ctl(EpollOp::EpollCtlDel,fd,None)
126+
self.epoll_ctl(EpollOp::EpollCtlDel, fd, None)
127127
}
128128
/// Change the settings associated with `fd` in the interest list to the new settings specified
129129
/// in `event`.
130-
///
130+
///
131131
/// [`epoll_ctl`](https://man7.org/linux/man-pages/man2/epoll_ctl.2.html) with `EPOLL_CTL_MOD`.
132-
pub fn modify<Fd: AsFd>(&self,fd: Fd, event: &mut EpollEvent) -> Result<()> {
133-
self.epoll_ctl(EpollOp::EpollCtlMod,fd,event)
132+
pub fn modify<Fd: AsFd>(
133+
&self,
134+
fd: Fd,
135+
event: &mut EpollEvent,
136+
) -> Result<()> {
137+
self.epoll_ctl(EpollOp::EpollCtlMod, fd, event)
134138
}
135139
/// Waits for I/O events, blocking the calling thread if no events are currently available.
136140
/// (This can be thought of as fetching items from the ready list of the epoll instance.)
137-
///
141+
///
138142
/// [`epoll_wait`](https://man7.org/linux/man-pages/man2/epoll_wait.2.html)
139-
pub fn wait(&self, events: &mut [EpollEvent], timeout: isize) -> Result<usize> {
143+
pub fn wait(
144+
&self,
145+
events: &mut [EpollEvent],
146+
timeout: isize,
147+
) -> Result<usize> {
140148
let res = unsafe {
141149
libc::epoll_wait(
142150
self.0.as_raw_fd(),
@@ -145,15 +153,15 @@ impl Epoll {
145153
timeout as c_int,
146154
)
147155
};
148-
156+
149157
Errno::result(res).map(|r| r as usize)
150158
}
151159
/// This system call is used to add, modify, or remove entries in the interest list of the epoll
152160
/// instance referred to by `self`. It requests that the operation `op` be performed for the
153161
/// target file descriptor, `fd`.
154-
///
162+
///
155163
/// When possible prefer [`Epoll::add`], [`Epoll::delete`] and [`Epoll::modify`].
156-
///
164+
///
157165
/// [`epoll_ctl`](https://man7.org/linux/man-pages/man2/epoll_ctl.2.html)
158166
fn epoll_ctl<'a, Fd: AsFd, T>(
159167
&self,
@@ -165,9 +173,17 @@ impl Epoll {
165173
T: Into<Option<&'a mut EpollEvent>>,
166174
{
167175
let event: Option<&mut EpollEvent> = event.into();
168-
let ptr = event.map(|x|&mut x.event as *mut libc::epoll_event).unwrap_or(std::ptr::null_mut());
176+
let ptr = event
177+
.map(|x| &mut x.event as *mut libc::epoll_event)
178+
.unwrap_or(std::ptr::null_mut());
169179
unsafe {
170-
Errno::result(libc::epoll_ctl(self.0.as_raw_fd(), op as c_int, fd.as_fd().as_raw_fd(), ptr)).map(drop)
180+
Errno::result(libc::epoll_ctl(
181+
self.0.as_raw_fd(),
182+
op as c_int,
183+
fd.as_fd().as_raw_fd(),
184+
ptr,
185+
))
186+
.map(drop)
171187
}
172188
}
173189
}
@@ -231,4 +247,4 @@ pub fn epoll_wait(
231247
};
232248

233249
Errno::result(res).map(|r| r as usize)
234-
}
250+
}

0 commit comments

Comments
 (0)