mirror of
https://github.com/fish-shell/fish-shell.git
synced 2024-12-24 18:53:51 +08:00
ce559bc20e
I needed to rename some types already ported to rust so they don't clash with their still-extant cpp counterparts. Helper ffi functions added to avoid needing to dynamically allocate an FdMonitorItem for every fd (we use dozens per basic prompt). I ported some functions from cpp to rust that are used only in the backend but without removing their existing cpp counterparts so cpp code can continue to use their version of them (`wperror` and `make_detached_pthread`). I ran into issues porting line-by-line logic because rust inverts the behavior of `std::remove_if(..)` by making it (basically) `Vec::retain_if(..)` so I replaced bools with an explict enum to make everything clearer. I'll port the cpp tests for this separately, for now they're using ffi. Porting closures was ugly. It's nothing hard, but it's very ugly as now each capturing lambda has been changed into an explicit struct that contains its parameters (that needs to be dynamically allocated), a standalone callback (member) function to replace the lambda contents, and a separate trampoline function to call it from rust over the shared C abi (not really relevant to x86_64 w/ its single calling convention but probably needed on other platforms). I don't like that `fd_monitor.rs` has its own `c_void`. I couldn't find a way to move that to `ffi.rs` but still get cxx bridge to consider it a shared POD. Every time I moved it to a different module, it would consider it to be an opaque rust type instead. I worry this means we're going to have multiple `c_void1`, `c_void2`, etc. types as we continue to port code to use function pointers. Also, rust treats raw pointers as foreign so you can't do `impl Send for * const Foo` even if `Foo` is from the same module. That necessitated a wrapper type (`void_ptr`) that implements `Send` and `Sync` so we can move stuff between threads. The code in fd_monitor_t has been split into two objects, one that is used by the caller and a separate one associated with the background thread (this is made nice and clean by rust's ownership model). Objects not needed under the lock (i.e. accessed by the background thread exclusively) were moved to the separate `BackgroundFdMonitor` type.
123 lines
2.8 KiB
Rust
123 lines
2.8 KiB
Rust
use crate::ffi;
|
|
use nix::unistd;
|
|
use std::os::unix::io::{AsRawFd, FromRawFd, RawFd};
|
|
|
|
/// A helper type for managing and automatically closing a file descriptor
|
|
///
|
|
/// This was implemented in rust as a port of the existing C++ code but it didn't take its place
|
|
/// (yet) and there's still the original cpp implementation in `src/fds.h`, so its name is
|
|
/// disambiguated because some code uses a mix of both for interop purposes.
|
|
pub struct AutoCloseFd {
|
|
fd_: RawFd,
|
|
}
|
|
|
|
#[cxx::bridge]
|
|
mod autoclose_fd_t {
|
|
extern "Rust" {
|
|
#[cxx_name = "autoclose_fd_t2"]
|
|
type AutoCloseFd;
|
|
|
|
#[cxx_name = "valid"]
|
|
fn is_valid(&self) -> bool;
|
|
fn close(&mut self);
|
|
fn fd(&self) -> i32;
|
|
}
|
|
}
|
|
|
|
impl AutoCloseFd {
|
|
// Closes the fd if not already closed.
|
|
pub fn close(&mut self) {
|
|
if self.fd_ != -1 {
|
|
_ = unistd::close(self.fd_);
|
|
self.fd_ = -1;
|
|
}
|
|
}
|
|
|
|
// Returns the fd.
|
|
pub fn fd(&self) -> RawFd {
|
|
self.fd_
|
|
}
|
|
|
|
// Returns the fd, transferring ownership to the caller.
|
|
pub fn acquire(&mut self) -> RawFd {
|
|
let temp = self.fd_;
|
|
self.fd_ = -1;
|
|
temp
|
|
}
|
|
|
|
// Resets to a new fd, taking ownership.
|
|
pub fn reset(&mut self, fd: RawFd) {
|
|
if fd == self.fd_ {
|
|
return;
|
|
}
|
|
self.close();
|
|
self.fd_ = fd;
|
|
}
|
|
|
|
// Returns if this has a valid fd.
|
|
pub fn is_valid(&self) -> bool {
|
|
self.fd_ >= 0
|
|
}
|
|
|
|
// Create a new AutoCloseFd instance taking ownership of the passed fd
|
|
pub fn new(fd: RawFd) -> Self {
|
|
AutoCloseFd { fd_: fd }
|
|
}
|
|
|
|
// Create a new AutoCloseFd without an open fd
|
|
pub fn empty() -> Self {
|
|
AutoCloseFd { fd_: -1 }
|
|
}
|
|
}
|
|
|
|
impl FromRawFd for AutoCloseFd {
|
|
unsafe fn from_raw_fd(fd: RawFd) -> Self {
|
|
AutoCloseFd { fd_: fd }
|
|
}
|
|
}
|
|
|
|
impl AsRawFd for AutoCloseFd {
|
|
fn as_raw_fd(&self) -> RawFd {
|
|
self.fd()
|
|
}
|
|
}
|
|
|
|
impl Default for AutoCloseFd {
|
|
fn default() -> AutoCloseFd {
|
|
AutoCloseFd { fd_: -1 }
|
|
}
|
|
}
|
|
|
|
impl Drop for AutoCloseFd {
|
|
fn drop(&mut self) {
|
|
self.close()
|
|
}
|
|
}
|
|
|
|
/// Helper type returned from make_autoclose_pipes.
|
|
#[derive(Default)]
|
|
pub struct autoclose_pipes_t {
|
|
/// Read end of the pipe.
|
|
pub read: AutoCloseFd,
|
|
|
|
/// Write end of the pipe.
|
|
pub write: AutoCloseFd,
|
|
}
|
|
|
|
/// Construct a pair of connected pipes, set to close-on-exec.
|
|
/// \return None on fd exhaustion.
|
|
pub fn make_autoclose_pipes() -> Option<autoclose_pipes_t> {
|
|
let pipes = ffi::make_pipes_ffi();
|
|
|
|
let readp = AutoCloseFd::new(pipes.read);
|
|
let writep = AutoCloseFd::new(pipes.write);
|
|
if !readp.is_valid() || !writep.is_valid() {
|
|
None
|
|
} else {
|
|
Some(autoclose_pipes_t {
|
|
read: readp,
|
|
write: writep,
|
|
})
|
|
}
|
|
}
|