From fff906a78ba545e77d087a82711b30bcc8e0d0c5 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Sun, 5 Apr 2020 14:22:59 +0200 Subject: Introduce SafePtr trait, cleanup --- safe_libc/src/boxed.rs | 104 +++++++++++++++++++------------------------------ src/main.rs | 31 +++++++++------ 2 files changed, 60 insertions(+), 75 deletions(-) diff --git a/safe_libc/src/boxed.rs b/safe_libc/src/boxed.rs index 625c5b0..fa3e45d 100644 --- a/safe_libc/src/boxed.rs +++ b/safe_libc/src/boxed.rs @@ -26,11 +26,40 @@ fn slice_len(p: *const [T]) -> usize { unsafe { mem::transmute::<_, [usize; 2]>(p)[1] } } -pub struct CBox(*mut T); +pub trait SafePtr { + fn safe_ptr(p: *mut Self) -> *mut Self; +} + +impl SafePtr for T { + #[inline] + fn safe_ptr(p: *mut T) -> *mut T { + if util::zst::(1) { + return dangling(); + } + + debug_assert!(!p.is_null(), "NULL ptr"); + p + } +} + +impl SafePtr for [T] { + #[inline] + fn safe_ptr(p: *mut [T]) -> *mut [T] { + let len = slice_len(p); + if util::zst::(len) { + return ptr::slice_from_raw_parts_mut(dangling(), len); + } -impl CBox { + debug_assert!(!p.is_null(), "NULL ptr"); + p + } +} + +pub struct CBox(*mut T); + +impl CBox { #[inline] - pub const unsafe fn from_raw_unchecked(p: *mut T) -> CBox { + pub unsafe fn from_raw_unchecked(p: *mut T) -> CBox { CBox(p) } @@ -42,7 +71,7 @@ impl CBox { } #[inline] - pub const fn as_ptr(&self) -> *const T { + pub fn as_ptr(&self) -> *const T { self.0 } @@ -78,32 +107,9 @@ impl CBox { util::check_ptr(p, len); CBox::slice_from_raw_parts_unchecked(p, len) } - - #[inline] - fn safe_ptr(&self) -> *mut T { - if util::zst::(1) { - return dangling(); - } - - debug_assert!(!self.0.is_null(), "NULL ptr"); - self.0 - } -} - -impl CBox<[T]> { - #[inline] - fn safe_ptr(&self) -> *mut [T] { - if self.0.is_null() { - let len = slice_len(self.0); - debug_assert!(util::zst::(len), "NULL ptr"); - return ptr::slice_from_raw_parts_mut(dangling(), len); - } - - self.0 - } } -impl Drop for CBox { +impl Drop for CBox { #[inline] fn drop(&mut self) { unsafe { @@ -113,60 +119,30 @@ impl Drop for CBox { } } -impl Deref for CBox { +impl Deref for CBox { type Target = T; #[inline] fn deref(&self) -> &T { - unsafe { &*self.safe_ptr() } + unsafe { &*T::safe_ptr(self.0) } } } -impl DerefMut for CBox { +impl DerefMut for CBox { #[inline] fn deref_mut(&mut self) -> &mut T { - unsafe { &mut *self.safe_ptr() } - } -} - -impl Deref for CBox<[T]> { - type Target = [T]; - - #[inline] - fn deref(&self) -> &[T] { - unsafe { &*self.safe_ptr() } - } -} - -impl DerefMut for CBox<[T]> { - #[inline] - fn deref_mut(&mut self) -> &mut [T] { - unsafe { &mut *self.safe_ptr() } - } -} - -impl fmt::Debug for CBox { - #[inline] - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - fmt::Debug::fmt(&**self, f) + unsafe { &mut *T::safe_ptr(self.0) } } } -impl fmt::Debug for CBox<[T]> where [T]: fmt::Debug { +impl fmt::Debug for CBox { #[inline] fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fmt::Debug::fmt(&**self, f) } } -impl fmt::Display for CBox { - #[inline] - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - fmt::Display::fmt(&**self, f) - } -} - -impl fmt::Display for CBox<[T]> where [T]: fmt::Display { +impl fmt::Display for CBox { #[inline] fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fmt::Display::fmt(&**self, f) diff --git a/src/main.rs b/src/main.rs index 944b7ab..c79f1da 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,17 +9,26 @@ use core::fmt::Write; #[no_mangle] pub extern "C" fn main(_nargs: libc::c_int, _args: *const *const libc::c_char) -> libc::c_int { let mut stdout = unsafe { libc::stdio::stdout() }; - let x = libc::string::CString::from("foo"); - let l = x.len(); - let y = x.into_raw(); - let z = unsafe { - libc::boxed::CBox::slice_from_raw_parts(y, l) - }; - let foo = cstr!("Foo!\n"); - stdout.puts(foo); - let _ = writeln!(stdout, "Foo: {} {} {}", z[0], z[1], z[2]); - let b = libc::boxed::CBox::new(42); - let _ = writeln!(stdout, "Bar: {}", b); + + { + let foo = cstr!("Foo!\n"); + stdout.puts(foo); + } + + { + let x = libc::string::CString::from("foo"); + let l = x.len(); + let y = x.into_raw(); + let z = unsafe { + libc::boxed::CBox::slice_from_raw_parts(y, l) + }; + let _ = writeln!(stdout, "Foo: {} {} {}", z[0], z[1], z[2]); + } + + { + let b = libc::boxed::CBox::new(42); + let _ = writeln!(stdout, "Bar: {}", b); + } 0 } -- cgit v1.2.3