From 5bff483fe1cdcbadb5b265a98c56dda508c31be3 Mon Sep 17 00:00:00 2001 From: ridiculousfish Date: Sun, 7 Jan 2024 17:23:02 -0800 Subject: [PATCH] Clean up io_chain in IoStreams Previously this was a pointer; now it can just be a reference. --- fish-rust/src/builtins/eval.rs | 2 +- fish-rust/src/builtins/shared.rs | 7 +------ fish-rust/src/builtins/source.rs | 12 +----------- fish-rust/src/builtins/tests/string_tests.rs | 4 +++- fish-rust/src/builtins/tests/test_tests.rs | 8 +++++--- fish-rust/src/exec.rs | 5 ++--- fish-rust/src/io.rs | 12 ++++++++---- fish-rust/src/parse_execution.rs | 3 ++- 8 files changed, 23 insertions(+), 30 deletions(-) diff --git a/fish-rust/src/builtins/eval.rs b/fish-rust/src/builtins/eval.rs index b8012434c..73a588dd3 100644 --- a/fish-rust/src/builtins/eval.rs +++ b/fish-rust/src/builtins/eval.rs @@ -15,7 +15,7 @@ pub fn eval(parser: &Parser, streams: &mut IoStreams, args: &mut [&wstr]) -> Opt let new_cmd = join_strings(&args[1..], ' '); // Copy the full io chain; we may append bufferfills. - let mut ios = unsafe { &*streams.io_chain }.clone(); + let mut ios = streams.io_chain.clone(); // If stdout is piped, then its output must go to the streams, not to the io_chain in our // streams, because the pipe may be intended to be consumed by a process which diff --git a/fish-rust/src/builtins/shared.rs b/fish-rust/src/builtins/shared.rs index 1cb6cf3d9..6063f7dc6 100644 --- a/fish-rust/src/builtins/shared.rs +++ b/fish-rust/src/builtins/shared.rs @@ -934,12 +934,7 @@ fn builtin_breakpoint( } let bpb = parser.push_block(Block::breakpoint_block()); - let mut empty_io_chain = IoChain::new(); - let io_chain = if streams.io_chain.is_null() { - &mut empty_io_chain - } else { - unsafe { &mut *streams.io_chain } - }; + let io_chain = &streams.io_chain; reader_read(parser, STDIN_FILENO, io_chain); parser.pop_block(bpb); Some(parser.get_last_status()) diff --git a/fish-rust/src/builtins/source.rs b/fish-rust/src/builtins/source.rs index 3d5267466..c608df842 100644 --- a/fish-rust/src/builtins/source.rs +++ b/fish-rust/src/builtins/source.rs @@ -1,7 +1,6 @@ use crate::{ common::{escape, scoped_push_replacer, FilenameRef}, fds::{wopen_cloexec, AutoCloseFd}, - io::IoChain, nix::isatty, parser::Block, reader::reader_read, @@ -102,16 +101,7 @@ pub fn source(parser: &Parser, streams: &mut IoStreams, args: &mut [&wstr]) -> O } parser.vars().set_argv(argv_list); - let empty_io_chain = IoChain::new(); - let mut retval = reader_read( - parser, - fd, - if !streams.io_chain.is_null() { - unsafe { &*streams.io_chain } - } else { - &empty_io_chain - }, - ); + let mut retval = reader_read(parser, fd, streams.io_chain); parser.pop_block(sb); diff --git a/fish-rust/src/builtins/tests/string_tests.rs b/fish-rust/src/builtins/tests/string_tests.rs index 951772b62..31f5cb806 100644 --- a/fish-rust/src/builtins/tests/string_tests.rs +++ b/fish-rust/src/builtins/tests/string_tests.rs @@ -1,3 +1,4 @@ +use crate::io::IoChain; use crate::tests::prelude::*; use crate::wchar::prelude::*; @@ -27,7 +28,8 @@ fn string_test(mut args: Vec<&wstr>, expected_rc: Option, expected_out: &ws let parser: &Parser = Parser::principal_parser(); let mut outs = OutputStream::String(StringOutputStream::new()); let mut errs = OutputStream::Null; - let mut streams = IoStreams::new(&mut outs, &mut errs); + let io_chain = IoChain::new(); + let mut streams = IoStreams::new(&mut outs, &mut errs, &io_chain); streams.stdin_is_directly_redirected = false; // read from argv instead of stdin let rc = string(parser, &mut streams, args.as_mut_slice()).expect("string failed"); diff --git a/fish-rust/src/builtins/tests/test_tests.rs b/fish-rust/src/builtins/tests/test_tests.rs index 1cf568462..03e433360 100644 --- a/fish-rust/src/builtins/tests/test_tests.rs +++ b/fish-rust/src/builtins/tests/test_tests.rs @@ -1,6 +1,6 @@ use crate::builtins::prelude::*; use crate::builtins::test::test as builtin_test; -use crate::io::OutputStream; +use crate::io::{IoChain, OutputStream}; use crate::tests::prelude::*; fn run_one_test_test_mbracket(expected: i32, lst: &[&str], bracket: bool) -> bool { @@ -22,7 +22,8 @@ fn run_one_test_test_mbracket(expected: i32, lst: &[&str], bracket: bool) -> boo let mut argv = argv.iter().map(|s| s.as_ref()).collect::>(); let mut out = OutputStream::Null; let mut err = OutputStream::Null; - let mut streams = IoStreams::new(&mut out, &mut err); + let io_chain = IoChain::new(); + let mut streams = IoStreams::new(&mut out, &mut err, &io_chain); let result: Option = builtin_test(parser, &mut streams, &mut argv); @@ -53,7 +54,8 @@ fn test_test_brackets() { let mut out = OutputStream::Null; let mut err = OutputStream::Null; - let mut streams = IoStreams::new(&mut out, &mut err); + let io_chain = IoChain::new(); + let mut streams = IoStreams::new(&mut out, &mut err, &io_chain); let args1 = &mut ["["L, "foo"L]; assert_eq!( diff --git a/fish-rust/src/exec.rs b/fish-rust/src/exec.rs index 7236fb7a3..c5164e9f1 100644 --- a/fish-rust/src/exec.rs +++ b/fish-rust/src/exec.rs @@ -1110,7 +1110,7 @@ fn get_performer_for_builtin(p: &Process, j: &Job, io_chain: &IoChain) -> Box Box Box = diff --git a/fish-rust/src/io.rs b/fish-rust/src/io.rs index 329e17742..aa86db431 100644 --- a/fish-rust/src/io.rs +++ b/fish-rust/src/io.rs @@ -965,8 +965,8 @@ pub struct IoStreams<'a> { pub out_is_redirected: bool, pub err_is_redirected: bool, - // Actual IO redirections. This is only used by the source builtin. Unowned. - pub io_chain: *mut IoChain, + // Actual IO redirections. This is only used by the source builtin. + pub io_chain: &'a IoChain, // The job group of the job, if any. This enables builtins which run more code like eval() to // share pgid. @@ -975,7 +975,11 @@ pub struct IoStreams<'a> { } impl<'a> IoStreams<'a> { - pub fn new(out: &'a mut OutputStream, err: &'a mut OutputStream) -> Self { + pub fn new( + out: &'a mut OutputStream, + err: &'a mut OutputStream, + io_chain: &'a IoChain, + ) -> Self { IoStreams { out, err, @@ -985,7 +989,7 @@ pub fn new(out: &'a mut OutputStream, err: &'a mut OutputStream) -> Self { err_is_piped: false, out_is_redirected: false, err_is_redirected: false, - io_chain: std::ptr::null_mut(), + io_chain, job_group: None, } } diff --git a/fish-rust/src/parse_execution.rs b/fish-rust/src/parse_execution.rs index 8e0f9af54..9f9d359d8 100644 --- a/fish-rust/src/parse_execution.rs +++ b/fish-rust/src/parse_execution.rs @@ -1317,7 +1317,8 @@ fn run_function_statement( trace_if_enabled_with_args(ctx.parser(), L!("function"), &arguments); let mut outs = OutputStream::Null; let mut errs = OutputStream::String(StringOutputStream::new()); - let mut streams = IoStreams::new(&mut outs, &mut errs); + let io_chain = IoChain::new(); + let mut streams = IoStreams::new(&mut outs, &mut errs, &io_chain); let mut shim_arguments: Vec<&wstr> = arguments .iter() .map(|s| truncate_at_nul(s.as_ref()))