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 +++++++++++++++++++------------------------------ 1 file changed, 40 insertions(+), 64 deletions(-) (limited to 'safe_libc') 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) -- cgit v1.2.3