From 8612d34996d9069f03949256daabf30e345c39d7 Mon Sep 17 00:00:00 2001 From: Fabian Boehm Date: Fri, 16 Aug 2024 11:00:08 +0200 Subject: [PATCH] Remove useless osttr->cstr->osstr roundtrip --- src/bin/fish.rs | 21 ++------------------- src/parser.rs | 7 +++---- 2 files changed, 5 insertions(+), 23 deletions(-) diff --git a/src/bin/fish.rs b/src/bin/fish.rs index 85c4898e6..0b2f32256 100644 --- a/src/bin/fish.rs +++ b/src/bin/fish.rs @@ -441,21 +441,6 @@ fn fish_parse_opt(args: &mut [WString], opts: &mut FishCmdOpts) -> ControlFlow CString { - // is there no better way to do this? - // this is - // CStr::from_bytes_until_nul(s.as_bytes()).unwrap() - // except we need to add the nul if it is not present - CString::new( - s.as_bytes() - .iter() - .cloned() - .take_while(|&c| c != b'\0') - .collect::>(), - ) - .unwrap() -} - fn main() { PROGRAM_NAME.set(L!("fish")).unwrap(); if !cfg!(small_main_stack) { @@ -617,8 +602,7 @@ fn throwing_main() -> i32 { // TODO: if-let-chains if opts.profile_startup_output.is_some() && opts.profile_startup_output != opts.profile_output { - let s = cstr_from_osstr(&opts.profile_startup_output.unwrap()); - parser.emit_profiling(s.as_bytes()); + parser.emit_profiling(&opts.profile_startup_output.unwrap()); // If we are profiling both, ensure the startup data only // ends up in the startup file. @@ -720,8 +704,7 @@ fn throwing_main() -> i32 { ); if let Some(profile_output) = opts.profile_output { - let s = cstr_from_osstr(&profile_output); - parser.emit_profiling(s.as_bytes()); + parser.emit_profiling(&profile_output); } history::save_all(); diff --git a/src/parser.rs b/src/parser.rs index c518e9018..fbb02805b 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -39,7 +39,6 @@ use std::cell::{Ref, RefCell, RefMut}; use std::ffi::{CStr, OsStr}; use std::num::NonZeroU32; use std::os::fd::{AsRawFd, OwnedFd, RawFd}; -use std::os::unix::prelude::OsStrExt; use std::rc::Rc; #[cfg(target_has_atomic = "64")] use std::sync::atomic::AtomicU64; @@ -976,17 +975,17 @@ impl Parser { } /// Output profiling data to the given filename. - pub fn emit_profiling(&self, path: &[u8]) { + pub fn emit_profiling(&self, path: &OsStr) { // Save profiling information. OK to not use CLO_EXEC here because this is called while fish is // exiting (and hence will not fork). - let f = match std::fs::File::create(OsStr::from_bytes(path)) { + let f = match std::fs::File::create(path) { Ok(f) => f, Err(err) => { FLOG!( warning, wgettext_fmt!( "Could not write profiling information to file '%s': %s", - &String::from_utf8_lossy(path), + path.to_string_lossy(), err.to_string() ) );