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
//!Collection of utilities to work with C stuff
#![warn(missing_docs)]
#![cfg_attr(feature = "cargo-clippy", allow(clippy::style))]
#![no_std]

mod sys;
pub use sys::*;

#[cfg(feature = "libc")]
pub mod locale;
#[cfg(feature = "libc")]
pub mod memory;
#[cfg(feature = "libc")]
pub mod env;
pub mod args;
pub use args::Args;

///Converts C string to Rust's, verifying it is UTF-8
///
///It is UB to pass non-C string as it requires \0
pub unsafe fn c_str_to_rust(ptr: *const u8) -> Result<&'static str, core::str::Utf8Error> {
    let len = strlen(ptr as *const i8);
    let parts = core::slice::from_raw_parts(ptr, len);
    core::str::from_utf8(parts)
}

///Converts C string to Rust's one assuming it is UTF-8
///
///It is UB to pass non-C string as it requires \0
pub unsafe fn c_str_to_rust_unchecked(ptr: *const u8) -> &'static str {
    let len = strlen(ptr as *const i8);
    let parts = core::slice::from_raw_parts(ptr, len);
    core::str::from_utf8_unchecked(parts)
}