From f5dffcc7f089b3f33db0a2f771d56f330609b7e4 Mon Sep 17 00:00:00 2001 From: Steve Lau Date: Fri, 9 Dec 2022 11:02:30 +0800 Subject: [PATCH 1/2] refactor: take AsFd by value --- src/kmod.rs | 2 +- src/sys/mman.rs | 2 +- src/sys/statfs.rs | 2 +- src/sys/termios.rs | 14 +++++++------- test/sys/test_termios.rs | 2 +- test/test.rs | 2 +- test/test_pty.rs | 2 +- 7 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/kmod.rs b/src/kmod.rs index d7146612d5..d3725c3f8a 100644 --- a/src/kmod.rs +++ b/src/kmod.rs @@ -80,7 +80,7 @@ libc_bitflags!( /// /// See [`man init_module(2)`](https://man7.org/linux/man-pages/man2/init_module.2.html) for more information. pub fn finit_module( - fd: &Fd, + fd: Fd, param_values: &CStr, flags: ModuleInitFlags, ) -> Result<()> { diff --git a/src/sys/mman.rs b/src/sys/mman.rs index deef7005b5..e689e06e04 100644 --- a/src/sys/mman.rs +++ b/src/sys/mman.rs @@ -421,7 +421,7 @@ pub unsafe fn mmap( length: NonZeroUsize, prot: ProtFlags, flags: MapFlags, - f: Option<&F>, + f: Option, offset: off_t, ) -> Result<*mut c_void> { let ptr = diff --git a/src/sys/statfs.rs b/src/sys/statfs.rs index 721d45cb21..5111df2e6e 100644 --- a/src/sys/statfs.rs +++ b/src/sys/statfs.rs @@ -740,7 +740,7 @@ pub fn statfs(path: &P) -> Result { /// # Arguments /// /// `fd` - File descriptor of any open file within the file system to describe -pub fn fstatfs(fd: &Fd) -> Result { +pub fn fstatfs(fd: Fd) -> Result { unsafe { let mut stat = mem::MaybeUninit::::uninit(); Errno::result(LIBC_FSTATFS(fd.as_fd().as_raw_fd(), stat.as_mut_ptr())) diff --git a/src/sys/termios.rs b/src/sys/termios.rs index 4cc635bc21..b0286f51f1 100644 --- a/src/sys/termios.rs +++ b/src/sys/termios.rs @@ -1143,7 +1143,7 @@ pub fn cfmakesane(termios: &mut Termios) { /// `tcgetattr()` returns a `Termios` structure with the current configuration for a port. Modifying /// this structure *will not* reconfigure the port, instead the modifications should be done to /// the `Termios` structure and then the port should be reconfigured using `tcsetattr()`. -pub fn tcgetattr(fd: &Fd) -> Result { +pub fn tcgetattr(fd: Fd) -> Result { let mut termios = mem::MaybeUninit::uninit(); let res = unsafe { @@ -1162,7 +1162,7 @@ pub fn tcgetattr(fd: &Fd) -> Result { /// takes affect at a time specified by `actions`. Note that this function may return success if /// *any* of the parameters were successfully set, not only if all were set successfully. pub fn tcsetattr( - fd: &Fd, + fd: Fd, actions: SetArg, termios: &Termios, ) -> Result<()> { @@ -1179,7 +1179,7 @@ pub fn tcsetattr( /// Block until all output data is written (see /// [tcdrain(3p)](https://pubs.opengroup.org/onlinepubs/9699919799/functions/tcdrain.html)). -pub fn tcdrain(fd: &Fd) -> Result<()> { +pub fn tcdrain(fd: Fd) -> Result<()> { Errno::result(unsafe { libc::tcdrain(fd.as_fd().as_raw_fd()) }).map(drop) } @@ -1188,7 +1188,7 @@ pub fn tcdrain(fd: &Fd) -> Result<()> { /// /// `tcflow()` suspends of resumes the transmission or reception of data for the given port /// depending on the value of `action`. -pub fn tcflow(fd: &Fd, action: FlowArg) -> Result<()> { +pub fn tcflow(fd: Fd, action: FlowArg) -> Result<()> { Errno::result(unsafe { libc::tcflow(fd.as_fd().as_raw_fd(), action as c_int) }) @@ -1200,7 +1200,7 @@ pub fn tcflow(fd: &Fd, action: FlowArg) -> Result<()> { /// /// `tcflush()` will discard data for a terminal port in the input queue, output queue, or both /// depending on the value of `action`. -pub fn tcflush(fd: &Fd, action: FlushArg) -> Result<()> { +pub fn tcflush(fd: Fd, action: FlushArg) -> Result<()> { Errno::result(unsafe { libc::tcflush(fd.as_fd().as_raw_fd(), action as c_int) }) @@ -1212,7 +1212,7 @@ pub fn tcflush(fd: &Fd, action: FlushArg) -> Result<()> { /// /// When using asynchronous data transmission `tcsendbreak()` will transmit a continuous stream /// of zero-valued bits for an implementation-defined duration. -pub fn tcsendbreak(fd: &Fd, duration: c_int) -> Result<()> { +pub fn tcsendbreak(fd: Fd, duration: c_int) -> Result<()> { Errno::result(unsafe { libc::tcsendbreak(fd.as_fd().as_raw_fd(), duration) }) @@ -1223,7 +1223,7 @@ feature! { #![feature = "process"] /// Get the session controlled by the given terminal (see /// [tcgetsid(3)](https://pubs.opengroup.org/onlinepubs/9699919799/functions/tcgetsid.html)). -pub fn tcgetsid(fd: &Fd) -> Result { +pub fn tcgetsid(fd: Fd) -> Result { let res = unsafe { libc::tcgetsid(fd.as_fd().as_raw_fd()) }; Errno::result(res).map(Pid::from_raw) diff --git a/test/sys/test_termios.rs b/test/sys/test_termios.rs index cf55b10cd8..83919378a7 100644 --- a/test/sys/test_termios.rs +++ b/test/sys/test_termios.rs @@ -8,7 +8,7 @@ use nix::sys::termios::{self, tcgetattr, LocalFlags, OutputFlags}; use nix::unistd::{read, write}; /// Helper function analogous to `std::io::Write::write_all`, but for `Fd`s -fn write_all(f: &Fd, buf: &[u8]) { +fn write_all(f: Fd, buf: &[u8]) { let mut len = 0; while len < buf.len() { len += write(f.as_fd().as_raw_fd(), &buf[len..]).unwrap(); diff --git a/test/test.rs b/test/test.rs index b139ec3a44..efd5fd2a6f 100644 --- a/test/test.rs +++ b/test/test.rs @@ -70,7 +70,7 @@ use std::os::unix::io::{AsFd, AsRawFd}; use std::path::PathBuf; /// Helper function analogous to `std::io::Read::read_exact`, but for `Fd`s -fn read_exact(f: &Fd, buf: &mut [u8]) { +fn read_exact(f: Fd, buf: &mut [u8]) { let mut len = 0; while len < buf.len() { // get_mut would be better than split_at_mut, but it requires nightly diff --git a/test/test_pty.rs b/test/test_pty.rs index e1286e553c..9ee94470ff 100644 --- a/test/test_pty.rs +++ b/test/test_pty.rs @@ -135,7 +135,7 @@ fn test_open_ptty_pair() { } /// Put the terminal in raw mode. -fn make_raw(fd: &Fd) { +fn make_raw(fd: Fd) { let mut termios = tcgetattr(fd).unwrap(); cfmakeraw(&mut termios); tcsetattr(fd, SetArg::TCSANOW, &termios).unwrap(); From fc59f20e19d3371bef7f0001b08c4cd4b298a5dd Mon Sep 17 00:00:00 2001 From: Steve Lau Date: Fri, 9 Dec 2022 11:08:17 +0800 Subject: [PATCH 2/2] fix --- test/test_pty.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/test_pty.rs b/test/test_pty.rs index 9ee94470ff..4cc6620c3c 100644 --- a/test/test_pty.rs +++ b/test/test_pty.rs @@ -136,9 +136,9 @@ fn test_open_ptty_pair() { /// Put the terminal in raw mode. fn make_raw(fd: Fd) { - let mut termios = tcgetattr(fd).unwrap(); + let mut termios = tcgetattr(&fd).unwrap(); cfmakeraw(&mut termios); - tcsetattr(fd, SetArg::TCSANOW, &termios).unwrap(); + tcsetattr(&fd, SetArg::TCSANOW, &termios).unwrap(); } /// Test `io::Read` on the PTTY master