use std::ffi::c_char;
use std::mem;
use crate::unsafe_std::ptrs::into_raw_ptr::IntoRawPtr;
use crate::unsafe_std::ptrs::raw_ptr::RawPtr;
#[repr(transparent)]
pub struct CString {
chars: RawPtr<c_char>
}
impl CString {
pub fn new_slice(str: &str) -> Self {
unsafe {
Self {
chars: RawPtr::<u8>::from( str.as_ptr()).cast()
}
}
}
pub unsafe fn new_owned(str: String) -> Self {
unsafe {
let c = Self {
chars: RawPtr::<u8>::from(str.as_ptr()).cast()
};
mem::forget(str);
c
}
}
pub unsafe fn free(&mut self) {
unsafe {
self.chars.free();
}
}
}
impl Copy for CString {}
impl Clone for CString {
fn clone(&self) -> Self {
Self {
chars: self.chars.clone()
}
}
}
unsafe impl IntoRawPtr for CString {
type Pointee = c_char;
#[inline(always)]
unsafe fn to_ptr(&self) -> RawPtr<Self::Pointee> {
self.chars
}
}