#![no_std]
#![warn(missing_docs)]
extern crate alloc;
use alloc::{format, string::String};
use core::slice::from_raw_parts;
use rcstring::{CString, Error};
#[inline]
pub fn with_cstring<T, E: From<Error>>(
s: String,
f: impl FnOnce(CString<'_>) -> Result<T, E>,
) -> Result<T, E> {
let string = format!("{}\0", s);
f(CString::new(&string)?)
}
#[inline]
pub fn from_cstring(cstring: &CString<'_>) -> String {
unsafe { from_cstring_raw(cstring.into_raw()) }
}
pub unsafe fn from_cstring_raw(cstring: *const libc::c_char) -> String {
String::from_utf8_lossy(from_raw_parts(cstring as *const u8, libc::strlen(cstring))).into()
}