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<Path>" which neither of CString and Vec<u8> do.
This commit is contained in:
Johannes Altmanninger 2023-04-09 13:53:01 +02:00
parent bfe68e6a83
commit b7638b50e4

View file

@ -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<u8> {
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();