Skip to content

Commit e11646e

Browse files
bors[bot]SteveLauC
andauthored
Merge #1932
1932: refactor: take `AsFd` by value r=asomers a=SteveLauC #### What this PR does 1. Changes the `fd` type to take `AsFd` by value for the I/O safety PRs that are merged. * #1916 * #1919 * #1921 * #1922 Co-authored-by: Steve Lau <[email protected]>
2 parents a2356ab + fc59f20 commit e11646e

File tree

7 files changed

+15
-15
lines changed

7 files changed

+15
-15
lines changed

src/kmod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ libc_bitflags!(
8080
///
8181
/// See [`man init_module(2)`](https://man7.org/linux/man-pages/man2/init_module.2.html) for more information.
8282
pub fn finit_module<Fd: AsFd>(
83-
fd: &Fd,
83+
fd: Fd,
8484
param_values: &CStr,
8585
flags: ModuleInitFlags,
8686
) -> Result<()> {

src/sys/mman.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ pub unsafe fn mmap<F: AsFd>(
421421
length: NonZeroUsize,
422422
prot: ProtFlags,
423423
flags: MapFlags,
424-
f: Option<&F>,
424+
f: Option<F>,
425425
offset: off_t,
426426
) -> Result<*mut c_void> {
427427
let ptr =

src/sys/statfs.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -740,7 +740,7 @@ pub fn statfs<P: ?Sized + NixPath>(path: &P) -> Result<Statfs> {
740740
/// # Arguments
741741
///
742742
/// `fd` - File descriptor of any open file within the file system to describe
743-
pub fn fstatfs<Fd: AsFd>(fd: &Fd) -> Result<Statfs> {
743+
pub fn fstatfs<Fd: AsFd>(fd: Fd) -> Result<Statfs> {
744744
unsafe {
745745
let mut stat = mem::MaybeUninit::<type_of_statfs>::uninit();
746746
Errno::result(LIBC_FSTATFS(fd.as_fd().as_raw_fd(), stat.as_mut_ptr()))

src/sys/termios.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -1143,7 +1143,7 @@ pub fn cfmakesane(termios: &mut Termios) {
11431143
/// `tcgetattr()` returns a `Termios` structure with the current configuration for a port. Modifying
11441144
/// this structure *will not* reconfigure the port, instead the modifications should be done to
11451145
/// the `Termios` structure and then the port should be reconfigured using `tcsetattr()`.
1146-
pub fn tcgetattr<Fd: AsFd>(fd: &Fd) -> Result<Termios> {
1146+
pub fn tcgetattr<Fd: AsFd>(fd: Fd) -> Result<Termios> {
11471147
let mut termios = mem::MaybeUninit::uninit();
11481148

11491149
let res = unsafe {
@@ -1162,7 +1162,7 @@ pub fn tcgetattr<Fd: AsFd>(fd: &Fd) -> Result<Termios> {
11621162
/// takes affect at a time specified by `actions`. Note that this function may return success if
11631163
/// *any* of the parameters were successfully set, not only if all were set successfully.
11641164
pub fn tcsetattr<Fd: AsFd>(
1165-
fd: &Fd,
1165+
fd: Fd,
11661166
actions: SetArg,
11671167
termios: &Termios,
11681168
) -> Result<()> {
@@ -1179,7 +1179,7 @@ pub fn tcsetattr<Fd: AsFd>(
11791179

11801180
/// Block until all output data is written (see
11811181
/// [tcdrain(3p)](https://pubs.opengroup.org/onlinepubs/9699919799/functions/tcdrain.html)).
1182-
pub fn tcdrain<Fd: AsFd>(fd: &Fd) -> Result<()> {
1182+
pub fn tcdrain<Fd: AsFd>(fd: Fd) -> Result<()> {
11831183
Errno::result(unsafe { libc::tcdrain(fd.as_fd().as_raw_fd()) }).map(drop)
11841184
}
11851185

@@ -1188,7 +1188,7 @@ pub fn tcdrain<Fd: AsFd>(fd: &Fd) -> Result<()> {
11881188
///
11891189
/// `tcflow()` suspends of resumes the transmission or reception of data for the given port
11901190
/// depending on the value of `action`.
1191-
pub fn tcflow<Fd: AsFd>(fd: &Fd, action: FlowArg) -> Result<()> {
1191+
pub fn tcflow<Fd: AsFd>(fd: Fd, action: FlowArg) -> Result<()> {
11921192
Errno::result(unsafe {
11931193
libc::tcflow(fd.as_fd().as_raw_fd(), action as c_int)
11941194
})
@@ -1200,7 +1200,7 @@ pub fn tcflow<Fd: AsFd>(fd: &Fd, action: FlowArg) -> Result<()> {
12001200
///
12011201
/// `tcflush()` will discard data for a terminal port in the input queue, output queue, or both
12021202
/// depending on the value of `action`.
1203-
pub fn tcflush<Fd: AsFd>(fd: &Fd, action: FlushArg) -> Result<()> {
1203+
pub fn tcflush<Fd: AsFd>(fd: Fd, action: FlushArg) -> Result<()> {
12041204
Errno::result(unsafe {
12051205
libc::tcflush(fd.as_fd().as_raw_fd(), action as c_int)
12061206
})
@@ -1212,7 +1212,7 @@ pub fn tcflush<Fd: AsFd>(fd: &Fd, action: FlushArg) -> Result<()> {
12121212
///
12131213
/// When using asynchronous data transmission `tcsendbreak()` will transmit a continuous stream
12141214
/// of zero-valued bits for an implementation-defined duration.
1215-
pub fn tcsendbreak<Fd: AsFd>(fd: &Fd, duration: c_int) -> Result<()> {
1215+
pub fn tcsendbreak<Fd: AsFd>(fd: Fd, duration: c_int) -> Result<()> {
12161216
Errno::result(unsafe {
12171217
libc::tcsendbreak(fd.as_fd().as_raw_fd(), duration)
12181218
})
@@ -1223,7 +1223,7 @@ feature! {
12231223
#![feature = "process"]
12241224
/// Get the session controlled by the given terminal (see
12251225
/// [tcgetsid(3)](https://pubs.opengroup.org/onlinepubs/9699919799/functions/tcgetsid.html)).
1226-
pub fn tcgetsid<Fd: AsFd>(fd: &Fd) -> Result<Pid> {
1226+
pub fn tcgetsid<Fd: AsFd>(fd: Fd) -> Result<Pid> {
12271227
let res = unsafe { libc::tcgetsid(fd.as_fd().as_raw_fd()) };
12281228

12291229
Errno::result(res).map(Pid::from_raw)

test/sys/test_termios.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use nix::sys::termios::{self, tcgetattr, LocalFlags, OutputFlags};
88
use nix::unistd::{read, write};
99

1010
/// Helper function analogous to `std::io::Write::write_all`, but for `Fd`s
11-
fn write_all<Fd: AsFd>(f: &Fd, buf: &[u8]) {
11+
fn write_all<Fd: AsFd>(f: Fd, buf: &[u8]) {
1212
let mut len = 0;
1313
while len < buf.len() {
1414
len += write(f.as_fd().as_raw_fd(), &buf[len..]).unwrap();

test/test.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ use std::os::unix::io::{AsFd, AsRawFd};
7070
use std::path::PathBuf;
7171

7272
/// Helper function analogous to `std::io::Read::read_exact`, but for `Fd`s
73-
fn read_exact<Fd: AsFd>(f: &Fd, buf: &mut [u8]) {
73+
fn read_exact<Fd: AsFd>(f: Fd, buf: &mut [u8]) {
7474
let mut len = 0;
7575
while len < buf.len() {
7676
// get_mut would be better than split_at_mut, but it requires nightly

test/test_pty.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,10 @@ fn test_open_ptty_pair() {
135135
}
136136

137137
/// Put the terminal in raw mode.
138-
fn make_raw<Fd: AsFd>(fd: &Fd) {
139-
let mut termios = tcgetattr(fd).unwrap();
138+
fn make_raw<Fd: AsFd>(fd: Fd) {
139+
let mut termios = tcgetattr(&fd).unwrap();
140140
cfmakeraw(&mut termios);
141-
tcsetattr(fd, SetArg::TCSANOW, &termios).unwrap();
141+
tcsetattr(&fd, SetArg::TCSANOW, &termios).unwrap();
142142
}
143143

144144
/// Test `io::Read` on the PTTY master

0 commit comments

Comments
 (0)