summaryrefslogtreecommitdiffstats
path: root/safe_libc/src/string.rs
diff options
context:
space:
mode:
Diffstat (limited to 'safe_libc/src/string.rs')
-rw-r--r--safe_libc/src/string.rs36
1 files changed, 20 insertions, 16 deletions
diff --git a/safe_libc/src/string.rs b/safe_libc/src/string.rs
index 6a8308a..5483e08 100644
--- a/safe_libc/src/string.rs
+++ b/safe_libc/src/string.rs
@@ -1,24 +1,26 @@
-use crate::{self as libc, util};
+use crate::{self as libc, boxed::CBox, util};
-use alloc::boxed::Box;
-use core::{ops::{Deref, DerefMut}, slice};
+use core::{mem, slice};
+use core::ops::{Deref, DerefMut};
//pub struct FromBytesWithNulError {}
-#[repr(transparent)]
-pub struct CStr {
- inner: libc::c_char,
+extern "C" {
+ type RawCStr;
}
+#[repr(transparent)]
+pub struct CStr(RawCStr);
+
impl CStr {
#[inline]
pub unsafe fn from_ptr<'a>(p: *const libc::c_char) -> &'a CStr {
- &*p.cast()
+ &*(p as *const CStr)
}
#[inline]
pub unsafe fn from_mut_ptr<'a>(p: *mut libc::c_char) -> &'a mut CStr {
- &mut *p.cast()
+ &mut *(p as *mut CStr)
}
#[inline]
@@ -37,12 +39,12 @@ impl CStr {
#[inline]
pub const fn as_ptr(&self) -> *const libc::c_char {
- &self.inner
+ (self as *const CStr).cast()
}
#[inline]
pub fn as_mut_ptr(&mut self) -> *mut libc::c_char {
- &mut self.inner
+ (self as *mut CStr).cast()
}
#[inline]
@@ -79,20 +81,22 @@ macro_rules! cstr {
}
pub struct CString {
- inner: Box<libc::c_char>,
+ inner: CBox<CStr>,
}
impl CString {
#[inline]
pub unsafe fn from_raw(p: *mut libc::c_char) -> CString {
CString {
- inner: Box::from_raw(p),
+ inner: CBox::from_raw_unchecked(p as *mut CStr)
}
}
#[inline]
- pub fn into_raw(self) -> *mut libc::c_char {
- Box::into_raw(self.inner)
+ pub fn into_raw(mut self) -> *mut libc::c_char {
+ let p = self.as_mut_ptr();
+ mem::forget(self);
+ p
}
}
@@ -101,14 +105,14 @@ impl Deref for CString {
#[inline]
fn deref(&self) -> &CStr {
- unsafe { CStr::from_ptr(&*self.inner) }
+ &*self.inner
}
}
impl DerefMut for CString {
#[inline]
fn deref_mut(&mut self) -> &mut CStr {
- unsafe { CStr::from_mut_ptr(&mut *self.inner) }
+ &mut *self.inner
}
}