lv_std/unsafe_std/c/
c_string.rs1use std::ffi::c_char;
2use std::mem;
3use crate::unsafe_std::ptrs::into_raw_ptr::IntoRawPtr;
4use crate::unsafe_std::ptrs::raw_ptr::RawPtr;
5
6#[repr(transparent)]
7pub struct CString {
8 chars: RawPtr<c_char>
9}
10
11impl CString {
12 pub fn new_slice(str: &str) -> Self {
13 unsafe {
14 Self {
15 chars: RawPtr::<u8>::from( str.as_ptr()).cast()
16 }
17 }
18 }
19
20 pub unsafe fn new_owned(str: String) -> Self {
22 unsafe {
23 let c = Self {
24 chars: RawPtr::<u8>::from(str.as_ptr()).cast()
25 };
26 mem::forget(str);
27 c
28 }
29 }
30
31 pub unsafe fn free(&mut self) {
32 unsafe {
33 self.chars.free();
34 }
35 }
36}
37
38impl Copy for CString {}
39
40impl Clone for CString {
41 fn clone(&self) -> Self {
42 Self {
43 chars: self.chars.clone()
44 }
45 }
46}
47
48unsafe impl IntoRawPtr for CString {
49 type Pointee = c_char;
50
51 #[inline(always)]
52 unsafe fn to_ptr(&self) -> RawPtr<Self::Pointee> {
53 self.chars
54 }
55}
56