use std::{
ffi::{CString, OsStr},
os::unix::ffi::OsStrExt,
};
pub trait ToCString {
fn to_cstring(&self) -> CString;
}
impl<T: AsRef<OsStr>> ToCString for T {
fn to_cstring(&self) -> CString {
#[expect(clippy::disallowed_methods)]
CString::new(self.as_ref().as_bytes()).unwrap()
}
}
#[cfg(test)]
mod tests {
use std::ffi::OsString;
use super::*;
#[test]
fn test_to_cstring_1() {
let s = "hello";
let cs = s.to_cstring();
assert_eq!(cs.to_str().unwrap(), "hello");
}
#[test]
fn test_to_cstring_2() {
let s = "";
let cs = s.to_cstring();
assert_eq!(cs.to_str().unwrap(), "");
}
#[test]
fn test_to_cstring_3() {
let s = OsString::from("test_path");
let cs = s.to_cstring();
assert_eq!(cs.to_str().unwrap(), "test_path");
}
#[test]
fn test_to_cstring_4() {
let s = "/usr/bin/syd";
let cs = s.to_cstring();
assert_eq!(cs.to_str().unwrap(), "/usr/bin/syd");
}
}