1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#![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()
}