cstring/
lib.rs

1#![no_std]
2mod cstr_core;
3extern crate alloc;
4use crate::alloc::borrow::ToOwned;
5use alloc::string::String;
6use core::convert::TryInto;
7
8pub fn from_str(s: &str) -> usize {
9    cstr_core::CString::new(s).unwrap().into_raw() as usize
10}
11
12pub fn try_into_string(start: impl TryInto<i32>) -> Result<String, &'static str> {
13    if let Ok(pos) = start.try_into() {
14        let s: &cstr_core::CStr =
15            unsafe { cstr_core::CStr::from_ptr(pos as *const cstr_core::c_char) };
16        if let Ok(s) = s.to_str() {
17            Ok(s.to_owned())
18        } else {
19            Err("error creating cstring")
20        }
21    } else {
22        Err("could not decypher cstring starting point")
23    }
24}