Minor cleanup, implement Debug, Display for CBox
This commit is contained in:
parent
f0b3d5166e
commit
de9bc974a6
5 changed files with 47 additions and 16 deletions
|
@ -8,7 +8,9 @@ edition = "2018"
|
||||||
safe_libc = { path = "safe_libc" }
|
safe_libc = { path = "safe_libc" }
|
||||||
|
|
||||||
[profile.dev]
|
[profile.dev]
|
||||||
|
lto = true
|
||||||
panic = "abort"
|
panic = "abort"
|
||||||
|
|
||||||
[profile.release]
|
[profile.release]
|
||||||
|
lto = true
|
||||||
panic = "abort"
|
panic = "abort"
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use crate::util;
|
use crate::util;
|
||||||
|
|
||||||
use core::{mem, ptr};
|
use core::{fmt, mem, ptr};
|
||||||
use core::ops::{Deref, DerefMut};
|
use core::ops::{Deref, DerefMut};
|
||||||
|
|
||||||
fn alloc<T>(len: usize) -> *mut T {
|
fn alloc<T>(len: usize) -> *mut T {
|
||||||
|
@ -11,26 +11,26 @@ fn alloc<T>(len: usize) -> *mut T {
|
||||||
let align = mem::align_of::<T>();
|
let align = mem::align_of::<T>();
|
||||||
util::must_succeed(
|
util::must_succeed(
|
||||||
unsafe {
|
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]
|
#[inline]
|
||||||
fn dangling<T>() -> *mut T {
|
const fn dangling<T>() -> *mut T {
|
||||||
mem::align_of::<T>() as *mut T
|
mem::align_of::<T>() as *mut T
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn slice_len<T>(p: *const [T]) -> usize {
|
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);
|
pub struct CBox<T: ?Sized>(*mut T);
|
||||||
|
|
||||||
impl<T: ?Sized> CBox<T> {
|
impl<T: ?Sized> CBox<T> {
|
||||||
#[inline]
|
#[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)
|
CBox(p)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -42,7 +42,7 @@ impl<T: ?Sized> CBox<T> {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn as_ptr(&self) -> *const T {
|
pub const fn as_ptr(&self) -> *const T {
|
||||||
self.0
|
self.0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -108,7 +108,7 @@ impl<T: ?Sized> Drop for CBox<T> {
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
unsafe {
|
unsafe {
|
||||||
ptr::drop_in_place(self.0);
|
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() }
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -22,7 +22,7 @@ impl OStream {
|
||||||
pub fn write(&mut self, b: &[u8]) {
|
pub fn write(&mut self, b: &[u8]) {
|
||||||
unsafe {
|
unsafe {
|
||||||
libc::fwrite(
|
libc::fwrite(
|
||||||
b.as_ptr() as *const libc::c_void,
|
b.as_ptr().cast(),
|
||||||
1,
|
1,
|
||||||
b.len(),
|
b.len(),
|
||||||
self.file,
|
self.file,
|
||||||
|
|
|
@ -12,7 +12,7 @@ pub struct CStr { inner: libc::c_char }
|
||||||
impl CStr {
|
impl CStr {
|
||||||
#[inline]
|
#[inline]
|
||||||
pub unsafe fn from_ptr_unchecked<'a>(p: *const libc::c_char) -> &'a CStr {
|
pub unsafe fn from_ptr_unchecked<'a>(p: *const libc::c_char) -> &'a CStr {
|
||||||
&*(p as *const CStr)
|
&*p.cast()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
|
@ -23,7 +23,7 @@ impl CStr {
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub unsafe fn from_mut_ptr_unchecked<'a>(p: *mut libc::c_char) -> &'a mut CStr {
|
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]
|
#[inline]
|
||||||
|
@ -34,7 +34,7 @@ impl CStr {
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub unsafe fn from_bytes_with_nul_unchecked(bytes: &[u8]) -> &CStr {
|
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
|
// TODO
|
||||||
|
@ -59,7 +59,7 @@ impl CStr {
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn as_bytes(&self) -> &[u8] {
|
pub fn as_bytes(&self) -> &[u8] {
|
||||||
unsafe { slice::from_raw_parts(
|
unsafe { slice::from_raw_parts(
|
||||||
self.as_ptr() as *const u8,
|
self.as_ptr().cast(),
|
||||||
self.len(),
|
self.len(),
|
||||||
) }
|
) }
|
||||||
}
|
}
|
||||||
|
@ -67,7 +67,7 @@ impl CStr {
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn as_mut_bytes(&mut self) -> &mut [u8] {
|
pub fn as_mut_bytes(&mut self) -> &mut [u8] {
|
||||||
unsafe { slice::from_raw_parts_mut(
|
unsafe { slice::from_raw_parts_mut(
|
||||||
self.as_mut_ptr() as *mut u8,
|
self.as_mut_ptr().cast(),
|
||||||
self.len(),
|
self.len(),
|
||||||
) }
|
) }
|
||||||
}
|
}
|
||||||
|
@ -126,7 +126,7 @@ impl From<&[u8]> for CString {
|
||||||
unsafe {
|
unsafe {
|
||||||
CString::from_raw_unchecked(
|
CString::from_raw_unchecked(
|
||||||
util::must_succeed(libc::strndup(
|
util::must_succeed(libc::strndup(
|
||||||
s.as_ptr() as *const libc::c_char,
|
s.as_ptr().cast(),
|
||||||
s.len() as libc::size_t,
|
s.len() as libc::size_t,
|
||||||
))
|
))
|
||||||
)
|
)
|
||||||
|
|
|
@ -15,10 +15,11 @@ pub extern "C" fn main(_nargs: libc::c_int, _args: *const *const libc::c_char) -
|
||||||
let z = unsafe {
|
let z = unsafe {
|
||||||
libc::boxed::CBox::slice_from_raw_parts(y, l)
|
libc::boxed::CBox::slice_from_raw_parts(y, l)
|
||||||
};
|
};
|
||||||
//let y = unsafe { c::string::CBox::from_raw(x) };
|
|
||||||
let foo = cstr!("Foo!\n");
|
let foo = cstr!("Foo!\n");
|
||||||
stdout.puts(foo);
|
stdout.puts(foo);
|
||||||
let _ = writeln!(stdout, "Foo: {} {} {}", z[0], z[1], z[2]);
|
let _ = writeln!(stdout, "Foo: {} {} {}", z[0], z[1], z[2]);
|
||||||
|
let b = libc::boxed::CBox::new(42);
|
||||||
|
let _ = writeln!(stdout, "Bar: {}", b);
|
||||||
0
|
0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Reference in a new issue