Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions phper/src/alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,34 @@ impl<T> EBox<T> {
Self { ptr: raw }
}

/// Constructs from a raw pointer with cast.
///
/// # Safety
///
/// Make sure the pointer is from `into_raw`, or created from `emalloc`.
pub(crate) unsafe fn from_raw_cast<U>(raw: *mut U) -> Self {
const {
assert!(size_of::<U>() == size_of::<T>());
}
Self { ptr: raw.cast() }
}

/// Consumes and returning a wrapped raw pointer.
///
/// Will leak memory.
pub fn into_raw(b: EBox<T>) -> *mut T {
ManuallyDrop::new(b).ptr
}

/// Consumes and returning a wrapped raw pointer with cast.
///
/// Will leak memory.
pub(crate) fn into_raw_cast<U>(b: EBox<T>) -> *mut U {
const {
assert!(size_of::<U>() == size_of::<T>());
}
ManuallyDrop::new(b).ptr.cast()
}
}

impl<T: fmt::Debug> fmt::Debug for EBox<T> {
Expand Down
6 changes: 3 additions & 3 deletions phper/src/arrays.rs
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ impl ToOwned for ZArr {
unsafe {
// TODO The source really immutable?
let dest = phper_zend_array_dup(self.as_ptr() as *mut _);
ZArray::from_raw(dest.cast())
ZArray::from_raw_cast(dest)
}
}
}
Expand All @@ -369,7 +369,7 @@ impl ToRefOwned for ZArr {
unsafe {
phper_zval_arr(val.as_mut_ptr(), self.as_mut_ptr());
phper_z_addref_p(val.as_mut_ptr());
ZArray::from_raw(val.as_mut_z_arr().unwrap().as_mut_ptr().cast())
ZArray::from_raw_cast(val.as_mut_z_arr().unwrap().as_mut_ptr())
}
}
}
Expand All @@ -395,7 +395,7 @@ impl ZArray {
pub fn with_capacity(n: usize) -> Self {
unsafe {
let ptr = phper_zend_new_array(n.try_into().unwrap());
Self::from_raw(ptr.cast())
Self::from_raw_cast(ptr)
}
}
}
Expand Down
8 changes: 3 additions & 5 deletions phper/src/classes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ impl ClassEntry {
// day of debugging time here).
let mut val = ManuallyDrop::new(val);
let ptr = phper_z_obj_p(val.as_mut_ptr());
Ok(ZObject::from_raw(ptr.cast()))
Ok(ZObject::from_raw_cast(ptr))
}
}
}
Expand Down Expand Up @@ -334,8 +334,7 @@ impl<T: 'static> StateClass<T> {
pub fn new_object(&self, arguments: impl AsMut<[ZVal]>) -> crate::Result<StateObject<T>> {
self.as_class_entry()
.new_object(arguments)
.map(ZObject::into_raw)
.map(|ptr| ptr.cast())
.map(ZObject::into_raw_cast)
.map(StateObject::<T>::from_raw_object)
}

Expand All @@ -345,8 +344,7 @@ impl<T: 'static> StateClass<T> {
pub fn init_object(&self) -> crate::Result<StateObject<T>> {
self.as_class_entry()
.init_object()
.map(ZObject::into_raw)
.map(|ptr| ptr.cast())
.map(ZObject::into_raw_cast)
.map(StateObject::<T>::from_raw_object)
}
}
Expand Down
4 changes: 2 additions & 2 deletions phper/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -651,7 +651,7 @@ impl ZFunc {
pub fn get_function_or_method_name(&self) -> ZString {
unsafe {
let s = phper_get_function_or_method_name(self.as_ptr());
ZString::from_raw(s.cast())
ZString::from_raw_cast(s)
}
}

Expand Down Expand Up @@ -890,7 +890,7 @@ pub(crate) fn call_raw_common(call_fn: impl FnOnce(&mut ZVal)) -> crate::Result<
if !eg!(exception).is_null() {
#[allow(static_mut_refs)]
let e = ptr::replace(&mut eg!(exception), null_mut());
let obj = ZObject::from_raw(e.cast());
let obj = ZObject::from_raw_cast(e);
match ThrowObject::new(obj) {
Ok(e) => return Err(e.into()),
Err(e) => return Err(e.into()),
Expand Down
4 changes: 2 additions & 2 deletions phper/src/objects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ impl ToRefOwned for ZObj {
unsafe {
phper_zval_obj(val.as_mut_ptr(), self.as_mut_ptr());
phper_z_addref_p(val.as_mut_ptr());
ZObject::from_raw(val.as_mut_z_obj().unwrap().as_mut_ptr().cast())
ZObject::from_raw_cast(val.as_mut_z_obj().unwrap().as_mut_ptr())
}
}
}
Expand Down Expand Up @@ -447,7 +447,7 @@ impl<T> StateObject<T> {

/// Converts into [ZObject].
pub fn into_z_object(self) -> ZObject {
unsafe { ZObject::from_raw(self.into_raw_object().cast()) }
unsafe { ZObject::from_raw_cast(self.into_raw_object()) }
}
}

Expand Down
8 changes: 4 additions & 4 deletions phper/src/strings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ impl ToRefOwned for ZStr {
fn to_ref_owned(&mut self) -> Self::Owned {
unsafe {
let ptr = phper_zend_string_copy(self.as_mut_ptr());
ZString::from_raw(ptr.cast())
ZString::from_raw_cast(ptr)
}
}
}
Expand All @@ -198,7 +198,7 @@ impl ZString {
s.len().try_into().unwrap(),
false.into(),
);
Self::from_raw(ptr.cast())
Self::from_raw_cast(ptr)
}
}

Expand All @@ -209,7 +209,7 @@ impl ZString {
let s = s.as_ref();
let ptr =
phper_zend_string_init(s.as_ptr().cast(), s.len().try_into().unwrap(), true.into());
Self::from_raw(ptr.cast())
Self::from_raw_cast(ptr)
}
}
}
Expand All @@ -222,7 +222,7 @@ impl Clone for ZString {
phper_zstr_len(self.as_ptr()).try_into().unwrap(),
false.into(),
);
Self::from_raw(ZStr::from_mut_ptr(ptr.cast()))
Self::from_raw_cast(ZStr::from_mut_ptr(ptr))
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions phper/src/values.rs
Original file line number Diff line number Diff line change
Expand Up @@ -736,7 +736,7 @@ impl From<ZString> for ZVal {
fn from(s: ZString) -> Self {
unsafe {
let mut val = MaybeUninit::<ZVal>::uninit();
phper_zval_str(val.as_mut_ptr().cast(), ZString::into_raw(s).cast());
phper_zval_str(val.as_mut_ptr().cast(), ZString::into_raw_cast(s));
val.assume_init()
}
}
Expand All @@ -746,7 +746,7 @@ impl From<ZArray> for ZVal {
fn from(arr: ZArray) -> Self {
unsafe {
let mut val = MaybeUninit::<ZVal>::uninit();
phper_zval_arr(val.as_mut_ptr().cast(), ZArray::into_raw(arr).cast());
phper_zval_arr(val.as_mut_ptr().cast(), ZArray::into_raw_cast(arr));
val.assume_init()
}
}
Expand Down
Loading