From b7638b50e489b364b155b413924225df5a5438ab Mon Sep 17 00:00:00 2001 From: Johannes Altmanninger Date: Sun, 9 Apr 2023 13:53:01 +0200 Subject: [PATCH] common.rs: convenience function to convert to OsString Even though we generally dont' want to use this type (because it's immutable), it can be advantageous when working with the std::fs API. This is because it implements "AsRef" which neither of CString and Vec do. --- fish-rust/src/common.rs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/fish-rust/src/common.rs b/fish-rust/src/common.rs index 0e89d6e99..8a6269f84 100644 --- a/fish-rust/src/common.rs +++ b/fish-rust/src/common.rs @@ -24,10 +24,11 @@ use num_traits::ToPrimitive; use once_cell::sync::Lazy; use std::cell::RefCell; use std::env; -use std::ffi::CString; +use std::ffi::{CString, OsString}; use std::mem::{self, ManuallyDrop}; use std::ops::{Deref, DerefMut}; use std::os::fd::AsRawFd; +use std::os::unix::prelude::OsStringExt; use std::path::PathBuf; use std::rc::Rc; use std::str::FromStr; @@ -1138,6 +1139,16 @@ pub fn wcs2string(input: &wstr) -> Vec { result } +pub fn wcs2osstring(input: &wstr) -> OsString { + if input.is_empty() { + return OsString::new(); + } + + let mut result = vec![]; + wcs2string_appending(&mut result, input); + OsString::from_vec(result) +} + pub fn wcs2zstring(input: &wstr) -> CString { if input.is_empty() { return CString::default();