From de9bc974a6b423b7f086af23e50c47a097dcd352 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Sat, 4 Apr 2020 17:13:36 +0200 Subject: Minor cleanup, implement Debug, Display for CBox --- safe_libc/src/boxed.rs | 44 ++++++++++++++++++++++++++++++++++++-------- safe_libc/src/stdio.rs | 2 +- safe_libc/src/string.rs | 12 ++++++------ 3 files changed, 43 insertions(+), 15 deletions(-) (limited to 'safe_libc') diff --git a/safe_libc/src/boxed.rs b/safe_libc/src/boxed.rs index 72c961c..625c5b0 100644 --- a/safe_libc/src/boxed.rs +++ b/safe_libc/src/boxed.rs @@ -1,6 +1,6 @@ use crate::util; -use core::{mem, ptr}; +use core::{fmt, mem, ptr}; use core::ops::{Deref, DerefMut}; fn alloc(len: usize) -> *mut T { @@ -11,26 +11,26 @@ fn alloc(len: usize) -> *mut T { let align = mem::align_of::(); util::must_succeed( unsafe { - libc::memalign(align as libc::size_t, size as libc::size_t) as *mut T + libc::memalign(align as libc::size_t, size as libc::size_t) } - ) + ).cast() } #[inline] -fn dangling() -> *mut T { +const fn dangling() -> *mut T { mem::align_of::() as *mut T } #[inline] fn slice_len(p: *const [T]) -> usize { - unsafe { mem::transmute::<*const [T], [usize; 2]>(p)[1] } + unsafe { mem::transmute::<_, [usize; 2]>(p)[1] } } pub struct CBox(*mut T); impl CBox { #[inline] - pub unsafe fn from_raw_unchecked(p: *mut T) -> CBox { + pub const unsafe fn from_raw_unchecked(p: *mut T) -> CBox { CBox(p) } @@ -42,7 +42,7 @@ impl CBox { } #[inline] - pub fn as_ptr(&self) -> *const T { + pub const fn as_ptr(&self) -> *const T { self.0 } @@ -108,7 +108,7 @@ impl Drop for CBox { fn drop(&mut self) { unsafe { ptr::drop_in_place(self.0); - libc::free(self.0 as *mut libc::c_void); + libc::free(self.0.cast()); } } } @@ -144,3 +144,31 @@ impl DerefMut for CBox<[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) + } +} + +impl fmt::Debug for CBox<[T]> where [T]: fmt::Debug { + #[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 { + #[inline] + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + fmt::Display::fmt(&**self, f) + } +} diff --git a/safe_libc/src/stdio.rs b/safe_libc/src/stdio.rs index 5f33f74..271f023 100644 --- a/safe_libc/src/stdio.rs +++ b/safe_libc/src/stdio.rs @@ -22,7 +22,7 @@ impl OStream { pub fn write(&mut self, b: &[u8]) { unsafe { libc::fwrite( - b.as_ptr() as *const libc::c_void, + b.as_ptr().cast(), 1, b.len(), self.file, diff --git a/safe_libc/src/string.rs b/safe_libc/src/string.rs index c85f788..8f973ad 100644 --- a/safe_libc/src/string.rs +++ b/safe_libc/src/string.rs @@ -12,7 +12,7 @@ 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 as *const CStr) + &*p.cast() } #[inline] @@ -23,7 +23,7 @@ impl CStr { #[inline] pub unsafe fn from_mut_ptr_unchecked<'a>(p: *mut libc::c_char) -> &'a mut CStr { - &mut *(p as *mut CStr) + &mut *p.cast() } #[inline] @@ -34,7 +34,7 @@ impl CStr { #[inline] pub unsafe fn from_bytes_with_nul_unchecked(bytes: &[u8]) -> &CStr { - CStr::from_ptr_unchecked(bytes.as_ptr() as *const libc::c_char) + CStr::from_ptr_unchecked(bytes.as_ptr().cast()) } // TODO @@ -59,7 +59,7 @@ impl CStr { #[inline] pub fn as_bytes(&self) -> &[u8] { unsafe { slice::from_raw_parts( - self.as_ptr() as *const u8, + self.as_ptr().cast(), self.len(), ) } } @@ -67,7 +67,7 @@ impl CStr { #[inline] pub fn as_mut_bytes(&mut self) -> &mut [u8] { unsafe { slice::from_raw_parts_mut( - self.as_mut_ptr() as *mut u8, + self.as_mut_ptr().cast(), self.len(), ) } } @@ -126,7 +126,7 @@ impl From<&[u8]> for CString { unsafe { CString::from_raw_unchecked( util::must_succeed(libc::strndup( - s.as_ptr() as *const libc::c_char, + s.as_ptr().cast(), s.len() as libc::size_t, )) ) -- cgit v1.2.3