diff options
Diffstat (limited to 'safe_libc/src/boxed.rs')
-rw-r--r-- | safe_libc/src/boxed.rs | 44 |
1 files changed, 36 insertions, 8 deletions
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<T>(len: usize) -> *mut T { @@ -11,26 +11,26 @@ fn alloc<T>(len: usize) -> *mut T { let align = mem::align_of::<T>(); 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<T>() -> *mut T { +const fn dangling<T>() -> *mut T { mem::align_of::<T>() as *mut T } #[inline] fn slice_len<T>(p: *const [T]) -> usize { - unsafe { mem::transmute::<*const [T], [usize; 2]>(p)[1] } + unsafe { mem::transmute::<_, [usize; 2]>(p)[1] } } pub struct CBox<T: ?Sized>(*mut T); impl<T: ?Sized> CBox<T> { #[inline] - pub unsafe fn from_raw_unchecked(p: *mut T) -> CBox<T> { + pub const unsafe fn from_raw_unchecked(p: *mut T) -> CBox<T> { CBox(p) } @@ -42,7 +42,7 @@ impl<T: ?Sized> CBox<T> { } #[inline] - pub fn as_ptr(&self) -> *const T { + pub const fn as_ptr(&self) -> *const T { self.0 } @@ -108,7 +108,7 @@ impl<T: ?Sized> Drop for CBox<T> { 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<T> DerefMut for CBox<[T]> { unsafe { &mut *self.safe_ptr() } } } + +impl<T: fmt::Debug> fmt::Debug for CBox<T> { + #[inline] + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + fmt::Debug::fmt(&**self, f) + } +} + +impl<T> 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<T: fmt::Display> fmt::Display for CBox<T> { + #[inline] + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + fmt::Display::fmt(&**self, f) + } +} + +impl<T> fmt::Display for CBox<[T]> where [T]: fmt::Display { + #[inline] + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + fmt::Display::fmt(&**self, f) + } +} |