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.rs72
1 files changed, 25 insertions, 47 deletions
diff --git a/safe_libc/src/string.rs b/safe_libc/src/string.rs
index 53f8f87..6a8308a 100644
--- a/safe_libc/src/string.rs
+++ b/safe_libc/src/string.rs
@@ -1,39 +1,29 @@
-use crate::{self as libc, util, boxed::CBox};
+use crate::{self as libc, util};
-use core::slice;
-use core::ops::{Deref, DerefMut};
+use alloc::boxed::Box;
+use core::{ops::{Deref, DerefMut}, slice};
//pub struct FromBytesWithNulError {}
#[repr(transparent)]
-pub struct CStr { inner: libc::c_char }
+pub struct CStr {
+ inner: libc::c_char,
+}
impl CStr {
#[inline]
- pub unsafe fn from_ptr_unchecked<'a>(p: *const libc::c_char) -> &'a CStr {
- &*p.cast()
- }
-
- #[inline]
pub unsafe fn from_ptr<'a>(p: *const libc::c_char) -> &'a CStr {
- util::check_ptr(p, 1);
- CStr::from_ptr_unchecked(p)
- }
-
- #[inline]
- pub unsafe fn from_mut_ptr_unchecked<'a>(p: *mut libc::c_char) -> &'a mut CStr {
- &mut *p.cast()
+ &*p.cast()
}
#[inline]
pub unsafe fn from_mut_ptr<'a>(p: *mut libc::c_char) -> &'a mut CStr {
- util::check_ptr(p, 1);
- CStr::from_mut_ptr_unchecked(p)
+ &mut *p.cast()
}
#[inline]
pub unsafe fn from_bytes_with_nul_unchecked(bytes: &[u8]) -> &CStr {
- CStr::from_ptr_unchecked(bytes.as_ptr().cast())
+ CStr::from_ptr(bytes.as_ptr().cast())
}
// TODO
@@ -57,18 +47,12 @@ impl CStr {
#[inline]
pub fn to_bytes(&self) -> &[u8] {
- unsafe { slice::from_raw_parts(
- self.as_ptr().cast(),
- self.len(),
- ) }
+ unsafe { slice::from_raw_parts(self.as_ptr().cast(), self.len()) }
}
#[inline]
pub fn to_mut_bytes(&mut self) -> &mut [u8] {
- unsafe { slice::from_raw_parts_mut(
- self.as_mut_ptr().cast(),
- self.len(),
- ) }
+ unsafe { slice::from_raw_parts_mut(self.as_mut_ptr().cast(), self.len()) }
}
#[inline]
@@ -89,28 +73,26 @@ impl CStr {
#[macro_export]
macro_rules! cstr {
- ($s:expr) => (
+ ($s:expr) => {
unsafe { $crate::string::CStr::from_bytes_with_nul_unchecked(concat!($s, "\0").as_bytes()) }
- )
+ };
}
-pub struct CString { inner: CBox<libc::c_char> }
+pub struct CString {
+ inner: Box<libc::c_char>,
+}
impl CString {
#[inline]
- pub unsafe fn from_raw_unchecked(p: *mut libc::c_char) -> CString {
- CString { inner: CBox::from_raw_unchecked(p) }
- }
-
- #[inline]
pub unsafe fn from_raw(p: *mut libc::c_char) -> CString {
- util::check_ptr(p, 1);
- CString::from_raw_unchecked(p)
+ CString {
+ inner: Box::from_raw(p),
+ }
}
#[inline]
pub fn into_raw(self) -> *mut libc::c_char {
- self.inner.into_raw()
+ Box::into_raw(self.inner)
}
}
@@ -133,12 +115,10 @@ impl DerefMut for CString {
impl From<&[u8]> for CString {
fn from(s: &[u8]) -> CString {
unsafe {
- CString::from_raw_unchecked(
- util::must_succeed(libc::strndup(
- s.as_ptr().cast(),
- s.len(),
- ))
- )
+ CString::from_raw(util::must_succeed(libc::strndup(
+ s.as_ptr().cast(),
+ s.len(),
+ )))
}
}
}
@@ -153,8 +133,6 @@ impl From<&str> for CString {
impl From<&CStr> for CString {
#[inline]
fn from(s: &CStr) -> CString {
- unsafe {
- CString::from_raw_unchecked(util::must_succeed(libc::strdup(s.as_ptr())))
- }
+ unsafe { CString::from_raw(util::must_succeed(libc::strdup(s.as_ptr()))) }
}
}