From 049121540400f9f0fe7bfda17358c0a7c238289c Mon Sep 17 00:00:00 2001 From: gwenn Date: Tue, 29 Aug 2023 19:06:59 +0200 Subject: [PATCH 1/2] [WIP] Bump nix dependency to version 0.27 --- Cargo.toml | 2 +- src/tty/unix.rs | 23 ++++++++++------------- 2 files changed, 11 insertions(+), 14 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 5be68e087..a92a2a847 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -42,7 +42,7 @@ regex = { version = "1.5.5", optional = true } rustyline-derive = { version = "0.9.0", optional = true, path = "rustyline-derive" } [target.'cfg(unix)'.dependencies] -nix = { version = "0.26", default-features = false, features = ["fs", "ioctl", "poll", "signal", "term"] } +nix = { version = "0.27", default-features = false, features = ["fs", "ioctl", "poll", "signal", "term"] } utf8parse = "0.2" skim = { version = "0.10", optional = true, default-features = false } signal-hook = { version = "0.3", optional = true, default-features = false } diff --git a/src/tty/unix.rs b/src/tty/unix.rs index 61a604b92..9df6059d6 100644 --- a/src/tty/unix.rs +++ b/src/tty/unix.rs @@ -3,7 +3,7 @@ use std::cmp; use std::collections::HashMap; use std::fs::{File, OpenOptions}; use std::io::{self, BufReader, ErrorKind, Read, Write}; -use std::os::unix::io::{AsRawFd, IntoRawFd, RawFd}; +use std::os::unix::io::{AsFd, BorrowedFd, IntoRawFd, RawFd}; use std::os::unix::net::UnixStream; use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::mpsc::{self, SyncSender}; @@ -181,11 +181,10 @@ pub struct PosixRawReader { key_map: PosixKeyMap, // external print reader pipe_reader: Option, - fds: FdSet, } -impl AsRawFd for PosixRawReader { - fn as_raw_fd(&self) -> RawFd { +impl AsFd for PosixRawReader { + fn as_fd(&self) -> BorrowedFd<'_> { self.tty_in.get_ref().fd } } @@ -235,7 +234,6 @@ impl PosixRawReader { parser: Parser::new(), key_map, pipe_reader, - fds: FdSet::new(), } } @@ -684,7 +682,7 @@ impl PosixRawReader { if n > 0 { return Ok(n as i32); } - let mut fds = [poll::PollFd::new(self.as_raw_fd(), PollFlags::POLLIN)]; + let mut fds = [poll::PollFd::new(self, PollFlags::POLLIN)]; let r = poll::poll(&mut fds, timeout_ms); match r { Ok(n) => Ok(n), @@ -700,24 +698,23 @@ impl PosixRawReader { } fn select(&mut self, single_esc_abort: bool) -> Result { - let tty_in = self.as_raw_fd(); + let tty_in = self.as_fd(); let sigwinch_pipe = self.tty_in.get_ref().sigwinch_pipe; let pipe_reader = self .pipe_reader .as_ref() - .map(|pr| pr.lock().unwrap().0.as_raw_fd()); + .map(|pr| pr.lock().unwrap().0.as_fd()); loop { - let mut readfds = self.fds; - readfds.clear(); + let mut readfds = FdSet::new(); if let Some(sigwinch_pipe) = sigwinch_pipe { readfds.insert(sigwinch_pipe); } - readfds.insert(tty_in); + readfds.insert(&tty_in); if let Some(pipe_reader) = pipe_reader { readfds.insert(pipe_reader); } if let Err(err) = select::select( - readfds.highest().map(|h| h + 1), + None, Some(&mut readfds), None, None, @@ -734,7 +731,7 @@ impl PosixRawReader { if sigwinch_pipe.map_or(false, |fd| readfds.contains(fd)) { self.tty_in.get_ref().sigwinch()?; return Err(ReadlineError::WindowResized); - } else if readfds.contains(tty_in) { + } else if readfds.contains(&tty_in) { // prefer user input over external print return self.next_key(single_esc_abort).map(Event::KeyPress); } else if let Some(ref pipe_reader) = self.pipe_reader { From 92f750b08edf89b191067cc9d9694bbdecd13e7c Mon Sep 17 00:00:00 2001 From: gwenn Date: Sat, 2 Sep 2023 11:15:59 +0200 Subject: [PATCH 2/2] Use unsafe borrow_raw method --- src/tty/unix.rs | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/src/tty/unix.rs b/src/tty/unix.rs index 9df6059d6..39bf961cc 100644 --- a/src/tty/unix.rs +++ b/src/tty/unix.rs @@ -3,7 +3,7 @@ use std::cmp; use std::collections::HashMap; use std::fs::{File, OpenOptions}; use std::io::{self, BufReader, ErrorKind, Read, Write}; -use std::os::unix::io::{AsFd, BorrowedFd, IntoRawFd, RawFd}; +use std::os::unix::io::{AsFd, AsRawFd, BorrowedFd, IntoRawFd, RawFd}; use std::os::unix::net::UnixStream; use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::mpsc::{self, SyncSender}; @@ -185,7 +185,8 @@ pub struct PosixRawReader { impl AsFd for PosixRawReader { fn as_fd(&self) -> BorrowedFd<'_> { - self.tty_in.get_ref().fd + let fd = self.tty_in.get_ref().fd; + unsafe { BorrowedFd::borrow_raw(fd) } } } @@ -699,27 +700,26 @@ impl PosixRawReader { fn select(&mut self, single_esc_abort: bool) -> Result { let tty_in = self.as_fd(); - let sigwinch_pipe = self.tty_in.get_ref().sigwinch_pipe; + let sigwinch_pipe = self + .tty_in + .get_ref() + .sigwinch_pipe + .map(|fd| unsafe { BorrowedFd::borrow_raw(fd) }); let pipe_reader = self .pipe_reader .as_ref() - .map(|pr| pr.lock().unwrap().0.as_fd()); + .map(|pr| pr.lock().unwrap().0.as_raw_fd()) + .map(|fd| unsafe { BorrowedFd::borrow_raw(fd) }); loop { let mut readfds = FdSet::new(); - if let Some(sigwinch_pipe) = sigwinch_pipe { + if let Some(ref sigwinch_pipe) = sigwinch_pipe { readfds.insert(sigwinch_pipe); } readfds.insert(&tty_in); - if let Some(pipe_reader) = pipe_reader { + if let Some(ref pipe_reader) = pipe_reader { readfds.insert(pipe_reader); } - if let Err(err) = select::select( - None, - Some(&mut readfds), - None, - None, - None, - ) { + if let Err(err) = select::select(None, Some(&mut readfds), None, None, None) { if err == Errno::EINTR && self.tty_in.get_ref().sigwinch()? { return Err(ReadlineError::WindowResized); } else if err != Errno::EINTR { @@ -728,7 +728,7 @@ impl PosixRawReader { continue; } }; - if sigwinch_pipe.map_or(false, |fd| readfds.contains(fd)) { + if sigwinch_pipe.map_or(false, |fd| readfds.contains(&fd)) { self.tty_in.get_ref().sigwinch()?; return Err(ReadlineError::WindowResized); } else if readfds.contains(&tty_in) {